第三种情况
- #include
- #include
- #include
- #include
-
- pthread_mutex_t mutex1, mutex2;
- void * workA(void * arg) {
- pthread_mutex_lock(&mutex1);
- sleep(1);
- pthread_mutex_lock(&mutex2);
-
- printf("workA......\n");
-
- pthread_mutex_unlock(&mutex2);
- pthread_mutex_unlock(&mutex1);
-
- return NULL;
- }
-
- void * workB(void * arg) {
- pthread_mutex_lock(&mutex2);
- sleep(1);
- pthread_mutex_lock(&mutex1);
-
- printf("workB......\n");
-
- pthread_mutex_unlock(&mutex1);
- pthread_mutex_unlock(&mutex2);
-
- return NULL;
- }
-
-
- int main() {
- pthread_mutex_init(&mutex1, NULL);
- pthread_mutex_init(&mutex2, NULL);
-
- pthread_t tid1, tid2;
- pthread_create(&tid1, NULL, workA, NULL);
- pthread_create(&tid2, NULL, workB, NULL);
-
- pthread_join(tid1, NULL);
- pthread_join(tid2, NULL);
-
- pthread_mutex_destroy(&mutex1);
- pthread_mutex_destroy(&mutex2);
-
-
- return 0;
- }