-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlthread_mutex_test.c
65 lines (43 loc) · 1.4 KB
/
lthread_mutex_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <pthread.h>
#include <stdio.h>
#define NLOOP 50000000
#include "lthread_mutex.h"
struct lthread_mutex mutex_lock_st;
static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
static uint64_t counter = 0;
void *doit(void *);
int main()
{
pthread_t tidA, tidB, tidC, tidD, tidE, tidF;
lthread_mutex_init("lthread_mutex_locktest", &mutex_lock_st);
pthread_create(&tidA, NULL, doit, NULL);
pthread_create(&tidB, NULL, doit, NULL);
pthread_create(&tidC, NULL, doit, NULL);
pthread_create(&tidD, NULL, doit, NULL);
pthread_create(&tidE, NULL, doit, NULL);
pthread_create(&tidF, NULL, doit, NULL);
/*wait for both threads to terminate*/
pthread_join(tidA, NULL);
pthread_join(tidB, NULL);
pthread_join(tidC, NULL);
pthread_join(tidD, NULL);
pthread_join(tidE, NULL);
pthread_join(tidF, NULL);
printf("counter: %ld\n", counter);
lthread_mutex_destroy(&mutex_lock_st);
return 0;
}
void *doit(void *arg)
{
uint64_t i, val;
uint64_t tid = pthread_self();
printf("tid:%x - %ld\n", (unsigned int)tid, (unsigned int)tid);
for (i=0; i<NLOOP; i++) {
//pthread_mutex_lock(&counter_mutex);
lthread_mutex_lock(&mutex_lock_st);
counter += 1;
lthread_mutex_unlock(&mutex_lock_st);
//pthread_mutex_unlock(&counter_mutex);
}
return NULL;
}