如何使用 lock_mutex 或 sleep 函数强制三个线程再次打印“hello world”?我已经完成了...
/* t2.c
synchronize threads through mutex and conditional variable
To compile use: gcc -o t2 t2.c -lpthread
*/
#include <stdio.h>
#include <pthread.h>
void hello(); // define three routines called by threads
void world();
void again(); /*new statment*/
/* global variable shared by threads */
pthread_mutex_t mutex; // mutex
pthread_cond_t done_hello; // conditional variable
int done = 0; // testing variable
int main(int argc, char* argv[])
{
pthread_t tid_hello, // thread id
tid_world, tid_again;
/* initialization on mutex and cond variable */
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&done_hello, NULL);
pthread_create(&tid_hello, NULL, (void*)&hello, NULL); //thread creation
pthread_create(&tid_world, NULL, (void*)&world, NULL); //thread creation
pthread_create(&tid_again, NULL, (void*)&again, NULL); //thread creation/*new statment*/
/* main waits for the three threads to finish by order */
pthread_join(tid_hello, NULL);
pthread_join(tid_world, NULL);
pthread_join(tid_again, NULL); /*new statment*/
printf("\n");
return 0;
}
void hello()
{
pthread_mutex_lock(&mutex);
printf(" hello");
fflush(stdout); // flush buffer to allow instant print out
done = 2;
pthread_cond_signal(&done_hello); // signal world() thread
pthread_mutex_unlock(&mutex); // unlocks mutex to allow world to print
return;
}
void world()
{
pthread_mutex_lock(&mutex);
/* world thread waits until done == 1. */
while (done == 1)
pthread_cond_wait(&done_hello, &mutex);
printf(" world");
fflush(stdout);
pthread_mutex_unlock(&mutex); // unlocks mutex
return;
}
void again() /*new function*/
{
pthread_mutex_lock(&mutex);
/* again thread waits until done == 0. */
while (done == 0)
pthread_cond_wait(&done_hello, &mutex);
printf(" again");
fflush(stdout);
pthread_mutex_unlock(&mutex); // unlocks mutex
return;
}
最佳答案
是的,可以。但您应该使用多个 pthread_cond_t
信号:
- 打印
“hello”
时发送 - 当
“world”
被打印时出现。
实际上,来自 man pthread_cond_wait()
:
The effect of using more than one mutex for concurrent pthread_cond_timedwait() or pthread_cond_wait() operations on the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits on the condition variable, and this (dynamic) binding shall end when the wait returns.
每个 world
和 again
函数都必须等待自己的信号。
#include <stdio.h>
#include <pthread.h>
void * hello(void*);
void * world(void*);
void * again(void*);
/* global variable shared by threads */
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t done_hello = PTHREAD_COND_INITIALIZER;
pthread_cond_t done_world = PTHREAD_COND_INITIALIZER;
int main(void)
{
pthread_t threads[3];
pthread_create(&threads[0], NULL, hello, NULL);
pthread_create(&threads[1], NULL, world, NULL);
pthread_create(&threads[2], NULL, again, NULL);
for(int i = 0; i < 3; ++i)
pthread_join(threads[i], NULL);
printf("\n");
return 0;
}
void * hello(void* foo)
{
pthread_mutex_lock(&mutex);
printf(" hello");
fflush(stdout);
pthread_cond_signal(&done_hello);
pthread_mutex_unlock(&mutex);
return NULL;
}
void * world(void* foo)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&done_hello, &mutex);
printf(" world");
fflush(stdout);
pthread_cond_signal(&done_world);
pthread_mutex_unlock(&mutex);
return NULL;
}
void * again(void* foo)
{
pthread_mutex_lock(&mutex);
pthread_cond_wait(&done_world, &mutex);
printf(" again");
fflush(stdout);
pthread_mutex_unlock(&mutex);
return NULL;
}
关于c - 我如何修改线程以打印 "hello world again"?在C中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58913994/