-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathadd.cpp
More file actions
59 lines (53 loc) · 1.22 KB
/
add.cpp
File metadata and controls
59 lines (53 loc) · 1.22 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
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>
#define NUMBER 4
int g_val = 100;
pthread_mutex_t mutex;//定义互斥锁
void *ThreadWork(void *arg)
{
int *p = (int*)arg;
pthread_detach(pthread_self());//自己分离自己,不用主线程回收它的资源了
while(1)
{
pthread_mutex_lock(&mutex);//加锁
if(g_val > 0)
{
printf("i am pid : %d,i get g_val : %d\n",(int)syscall(SYS_gettid),g_val);
--g_val;
usleep(2);
}
else{
pthread_mutex_unlock(&mutex);//在所有可能退出的地方,进行解锁
break;
}
pthread_mutex_unlock(&mutex);//解锁
}
pthread_exit(NULL);
}
int main()
{
pthread_t tid[NUMBER];
pthread_mutex_init(&mutex,NULL);//互斥锁初始化
int i = 0;
for(;i < NUMBER;++i)
{
int ret = pthread_create(&tid[i],NULL,ThreadWork,(void*)&g_val);//不要传临时变量,这里是示范
if(ret != 0)
{
perror("pthread_create");
return -1;
}
}
//pthread_join(tid,NULL);//线程等待
//pthread_detach(tid);//线程分离
pthread_mutex_destroy(&mutex);//销毁互斥锁
while(1)
{
printf("i am main work thread\n");
sleep(1);
}
return 0;
}