6 int t = 0;
7 pthread_mutex_t mutex;
8
9 void my_lock(pthread_mutex_t *mutex)
10 {
11 pthread_mutex_lock(&mutex,NULL);
12 }
13
14 void my_unlock(pthread_mutex_t *mutex)
15 {
16 pthread_mutex_unlock(&mutex);
17 }
18
19 void * thread_func(void *arg)
20 {
21 // pthread_mutex_lock(&mutex);
22 my_lock(&mutex); //把这个注释掉换成上面的就不死锁
23 int *i = (int *)&arg;
24 sleep(1);
25 printf("t=%d,i=%d----",t,*i);
26 int num = t + *i;
27 t++;
28 printf("this is test:%d\n",num);
29 // pthread_mutex_unlock(&mutex);
30 my_unlock(&mutex); //把这个注释掉换成上面的就不死锁
31 char cmd[1024] = "diff -w /home/node104/jzhe/he/code/1.txt /home/node104/jzhe/he/code/2.txt";
32 system(cmd);
33 return 0;
34 }
35
36 int main()
37 {
38 pthread_mutex_init(&mutex,NULL);
39 pid_t thread[10];
40 int i = 0 ;
41 int j = 0;
42 int thread_num = 5;
43 for(i=0; i<thread_num; i++)
44 {
45 pid_t pid;
46 pthread_create(&thread[i],NULL,thread_func,(void *)i);
47 }
48 for(i=0; i<thread_num; i++)
49 {
50 pthread_join(thread[i],NULL);
51 }
52 }
------解决方案--------------------
- C/C++ code
void my_lock(pthread_mutex_t *mutex) { pthread_mutex_lock(mutex); // 去掉& } void my_unlock(pthread_mutex_t *mutex) { pthread_mutex_unlock(mutex); // 去掉& }