linux2次锁互斥变量死锁的例子

linux2次锁互斥变量死锁的例子
--------------------------------------------------


  1 #include <stdio.h>
  2 #include <pthread.h>
  3 #include <stdlib.h>
  4
  5
  6 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  7
  8 void* f()
  9 {
 10   pthread_mutex_lock(&mutex);
 11   //the second lock will caused dead lock in the thread  
 12   pthread_mutex_lock(&mutex);
 13   printf("new thread didn't dead lock?\n");
 14   pthread_mutex_unlock(&mutex);
 15   return (void*)0;
 16 }
 17
 18 int main()
 19 {
 20   //can not init in main function ?
 21   //mutex = PTHREAD_MUTEX_INITIALIZER;
 22   pthread_t ptd;
 23   pthread_create(&ptd, NULL, f, NULL);
 24   pthread_join(ptd, NULL);
 25   printf("main thread quit!\n");
 26   return 0;
 27 }
~                                                                                                                     

                       
~                


你可能感兴趣的:(JOIN,thread,linux,function,null)