I wrote a program ,which has two threads(A,B) and one mutex. Thread A :loop (mutex_lock -> some process ->mutex_unlock ) Thread B :mutex_lock when needed -> some process ->mutex_unlock I expected that Thread B can get mutex_lock when thread A unlock,but Thread B don't get any locks .gcc is 4.1.1. /linux kernel 2.6.21.
On the other hand, thread B can get lock on gcc 3.2.2/kernel 2.4. This is right behaviour? or i should not expect thread B can get lock? test source: #include <ifaddrs.h> #include <stdlib.h> #include <stdio.h> #include <time.h> #include <string.h> #include <sys/stat.h> #include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include <net/if.h> #include <pthread.h> pthread_mutex_t mutex_share; //pthread_mutex_t mutex_share = PTHREAD_MUTEX_INITIALIZER; void t1(); void t2(); int32_t main( int argc, char **argv){ int32_t ret = 0; pthread_t thread_hd; ret = pthread_mutex_init(&mutex_share, NULL); if (ret != 0) { fprintf(stderr, "init error\n"); } ret = pthread_create(&thread_hd, NULL, (void *)t1, 0); ret = pthread_create(&thread_hd, NULL, (void *)t2, 0); pause(); exit(0); } void t1(){ struct timeval to1; int32_t ret = 0; while(1) { ret = pthread_mutex_lock(&mutex_share); if (ret != 0){ fprintf(stderr, "t1 lock error\n"); } else { fprintf(stderr, "t1 locked\n"); } to1.tv_sec = 0; to1.tv_usec = 100000; select(0, NULL, NULL,NULL, &to1); ret = pthread_mutex_unlock(&mutex_share); if (ret != 0) { fprintf(stderr, "t1 unlock error\n"); } else { fprintf(stderr, "t1 unlocked\n"); } /* to1.tv_sec = 0; to1.tv_usec = 1000; select(0, NULL, NULL,NULL, &to1); */ } } void t2(){ struct timeval to2; int32_t ret = 0; while(1) { fprintf(stderr, "t2 locking.................. \n"); ret = pthread_mutex_lock(&mutex_share); if (ret != 0){ fprintf(stderr, "t2 lock error\n"); } else { fprintf(stderr, "t2 locked\n"); } /* to2.tv_sec = 0; to2.tv_usec = 100000; select(0, NULL, NULL,NULL, &to2); */ ret = pthread_mutex_unlock(&mutex_share); if (ret != 0){ fprintf(stderr, "t2 unlock error\n"); } else { fprintf(stderr, "t2 unlocked\n"); } sleep(1); } }