例如,我想创建5个线程并打印它们。如何让第四个在第二个之前执行?我尝试用互斥锁锁定它,但我不知道如何只锁定第二个,所以它给了我分段错误。
char name1[]="THREAD1";
char name2[]="THREAD2";
char name3[]="THREAD3";
char name4[]="THREAD4";
char name5[]="THREAD5";
pthread_mutex_t mutex;
pthread_t t1, t2,t3,t4,t5;
int* state[5]={0,0,0,0,0};
void* execThread(void* threadName)
{
int i;
int* number = (int*) malloc(sizeof(int));
char* tmp;
if(*(state[4]) == 0 && (pthread_self()==t2))
pthread_mutex_lock (&mutex);
else if(*(state[4]) == 0 && (pthread_self()!=t2))
{
pthread_yield();
printf("[Thread %s] Executing step %d\n", (char *) threadName, i);
tmp = & ((char*)threadName)[strlen((char*)threadName)-1];
*number = atoi(tmp);
return number;
}
else
{
pthread_mutex_unlock (&mutex);
pthread_yield();
printf("[Thread %s] Executing step %d\n", (char *) threadName, i);
tmp = & ((char*)threadName)[strlen((char*)threadName)-1];
*number = atoi(tmp);
return number;
}
}
void main()
{
pthread_mutex_init(&mutex, NULL);
printf("[Thread MAIN] Starting. tid=%ld, pid=%d\n", (long int) pthread_self(), (int) getpid());
if (pthread_create(&t1, NULL, execThread, name1)!=0) {
perror("Error creating a new thread");
exit(1);
}
if (pthread_create(&t2, NULL, execThread, name2)!=0) {
perror("Error creating a new thread");
exit(1);
}
if (pthread_create(&t3, NULL, execThread, name3)!=0) {
perror("Error creating a new thread");
exit(1);
}
if (pthread_create(&t4, NULL, execThread, name4)!=0) {
perror("Error creating a new thread");
exit(1);
}
if (pthread_create(&t5, NULL, execThread, name5)!=0) {
perror("Error creating a new thread");
exit(1);
}
printf("[Thread MAIN] Created 5 threads \n" );
pthread_join(t1, (void**) &state[0]);
pthread_join(t2, (void**) &state[1]);
pthread_join(t3, (void**) &state[2]);
pthread_join(t4, (void**) &state[3]);
pthread_join(t5, (void**) &state[4]);
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name1, (long int) t1, *(state[0]));
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name2, (long int) t2, *(state[1]));
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name3, (long int) t3, *(state[2]));
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name4, (long int) t4, *(state[3]));
printf("[Thread MAIN] Thread %s[%ld] terminated with state %d\n", name5, (long int) t5, *(state[4]));
printf("[Thread MAIN] Terminating. tid=%ld, pid=%d\n", (long int) pthread_self(), (int) getpid());
pthread_mutex_destroy(&mutex);
//pthread_exit(NULL);
}
答案 0 :(得分:0)
如果希望线程2仅在线程4打印其消息后打印其消息,则可以使用标志和条件变量。请注意,您无法安全地访问线程中的t1
,t2
等变量而无需进一步同步(它们由主线程编写,您尝试从其他线程读取它们) )。
int t4_done = 0;
pthread_mutex_t t4_done_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t t4_done_cond = PTHREAD_COND_INITIALIZER;
void* execThread(void* threadName)
{
if (threadName == name2)
{
/* thread2 waits for thread4 to set the t4_done flag */
pthread_mutex_lock(&t4_done_mutex);
while (!t4_done)
pthread_cond_wait(&t4_done_cond, &t4_done_mutex);
pthread_mutex_unlock(&t4_done_mutex);
}
printf("[Thread %s] Executing step\n", (char *) threadName);
if (threadName == name4)
{
/* thread4 sets the t4_done flag and signals thread2 */
pthread_mutex_lock(&t4_done_mutex);
t4_done = 1;
pthread_cond_signal(&t4_done_cond);
pthread_mutex_unlock(&t4_done_mutex);
}
return NULL;
}