请看下述代码是否有问题
疑问点:是否可以主线程触发->A->B->C->A(->表示pthread_cond_signal)
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
using namespace std;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
int count = 10;
int thd_id = 0;
void * thread_func1(void * arg)
{
for(int i = 0; i < count; i++)
{
pthread_mutex_lock(&mutex1);
pthread_cond_wait(&cond1, &mutex1);
printf("A");
pthread_mutex_unlock(&mutex1);
// notify B
pthread_cond_signal(&cond2);
}
return (void*)0;
}
void * thread_func2(void * arg)
{
for(int i = 0; i < count; i++)
{
pthread_mutex_lock(&mutex2);
pthread_cond_wait(&cond2, &mutex2);
printf("B");
pthread_mutex_unlock(&mutex2);
// notify C
pthread_cond_signal(&cond3);
}
return (void*)0;
}
void * thread_func3(void * arg)
{
for(int i = 0; i < count; i++)
{
pthread_mutex_lock(&mutex3);
pthread_cond_wait(&cond3, &mutex3);
printf("C");
pthread_mutex_unlock(&mutex3);
// notify A
pthread_cond_signal(&cond1);
}
return (void*)0;
}
int main()
{
pthread_t thd1, thd2, thd3;
if(pthread_create(&thd1, NULL, thread_func1, NULL) != 0)
{
printf("Create thread failed.\n");
}
if(pthread_create(&thd2, NULL, thread_func2, NULL) != 0)
{
printf("Create thread failed.\n");
}
if(pthread_create(&thd3, NULL, thread_func3, NULL) != 0)
{
printf("Create thread failed.\n");
}
// first notify A
pthread_cond_signal(&cond1);
void *ret;
pthread_join(thd1, &ret);
pthread_join(thd2, &ret);
pthread_join(thd3, &ret);
return 0;
}