-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathmutex.c
More file actions
52 lines (45 loc) · 1.14 KB
/
mutex.c
File metadata and controls
52 lines (45 loc) · 1.14 KB
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREAD 100
void *thread_inc(void *arg);
void *thread_des(void *arg);
long long num = 0;
pthread_mutex_t mutex; //保存互斥量读取值的变量
int main(int argc, char *argv[])
{
pthread_t thread_id[NUM_THREAD];
int i;
pthread_mutex_init(&mutex, NULL); //创建互斥量
for (i = 0; i < NUM_THREAD; i++)
{
if (i % 2)
pthread_create(&(thread_id[i]), NULL, thread_inc, NULL);
else
pthread_create(&(thread_id[i]), NULL, thread_des, NULL);
}
for (i = 0; i < NUM_THREAD; i++)
pthread_join(thread_id[i], NULL);
printf("result: %lld \n", num);
pthread_mutex_destroy(&mutex); //销毁互斥量
return 0;
}
void *thread_inc(void *arg)
{
int i;
pthread_mutex_lock(&mutex); //上锁
for (i = 0; i < 50000000; i++)
num += 1;
pthread_mutex_unlock(&mutex); //解锁
return NULL;
}
void *thread_des(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
for (i = 0; i < 50000000; i++)
num -= 1;
pthread_mutex_unlock(&mutex);
return NULL;
}