-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpthreadTest3.c
More file actions
54 lines (44 loc) · 820 Bytes
/
pthreadTest3.c
File metadata and controls
54 lines (44 loc) · 820 Bytes
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
#include "stdio.h"
#include <pthread.h>
pthread_mutex_t MyMutex;
long a[100000] = {0};
int i =0,j=0;
void* ThreadAProc()
{
while(1)
{
printf("add node %d to list\n",i);
pthread_mutex_lock(&MyMutex);
a[i] = i;
i++;
pthread_mutex_unlock(&MyMutex);
sleep(3);
}
return NULL;
}
void* ThreadBProc()
{
while(1)
{
printf("delete node %d from list\n",a[j]);
pthread_mutex_lock(&MyMutex);
j++;
pthread_mutex_unlock(&MyMutex);
sleep(5);
}
return NULL;
}
int main()
{
int k = 0;
pthread_t thread1;
pthread_t thread2;
int ret1;
int ret2;
pthread_mutex_init(&MyMutex,NULL);
ret1 = pthread_create(&thread1,NULL,&ThreadAProc,NULL);
ret2 = pthread_create(&thread2,NULL,&ThreadBProc,NULL);
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_mutex_destroy(&MyMutex);
}