#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t g_mutex_lock;
pthread_mutex_t g_mutex_lock2;
void *func1(void* args)
{
printf("%s(),LINE=%d\n",__FUNCTION__,__LINE__);
pthread_mutex_lock(&g_mutex_lock);
sleep(1);
pthread_mutex_lock(&g_mutex_lock2);
printf("%s(),LINE=%d\n",__FUNCTION__,__LINE__);
pthread_mutex_unlock(&g_mutex_lock);
pthread_mutex_unlock(&g_mutex_lock2);
return NULL;
}
void *func2(void* args)
{
printf("%s(),LINE=%d\n",__FUNCTION__,__LINE__);
pthread_mutex_lock(&g_mutex_lock2);
sleep(1);
pthread_mutex_lock(&g_mutex_lock);
printf("%s(),LINE=%d\n",__FUNCTION__,__LINE__);
pthread_mutex_unlock(&g_mutex_lock2);
return NULL;
}
int main(void)
{
int ret;
pthread_t thread_1;
pthread_t thread_2;
printf("%s(),LINE=%d\n",__FUNCTION__,__LINE__);
ret = pthread_mutex_init(&g_mutex_lock, NULL);
if (ret != 0) {
printf("mutex init failed\n");
return -1;
}
ret = pthread_mutex_init(&g_mutex_lock2, NULL);
if (ret != 0) {
printf("mutex2 init failed\n");
return -1;
}
pthread_create(&thread_1, NULL, func1, NULL);
pthread_create(&thread_2, NULL, func2, NULL);
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
pthread_mutex_destroy(&g_mutex_lock);
pthread_mutex_destroy(&g_mutex_lock2);
printf("%s(),LINE=%d\n",__FUNCTION__,__LINE__);
return 0;
}