【发布时间】:2018-09-11 11:42:06
【问题描述】:
我正在尝试了解线程编程中的条件变量,谁能解释它如何与互斥锁一起工作以及条件变量是什么,以便它可以同步线程? 条件变量和线程
【问题讨论】:
标签: c linux synchronization pthreads
我正在尝试了解线程编程中的条件变量,谁能解释它如何与互斥锁一起工作以及条件变量是什么,以便它可以同步线程? 条件变量和线程
【问题讨论】:
标签: c linux synchronization pthreads
给你。在这里,我使用了 3 个互斥锁和 3 个条件变量。通过以下示例,您可以在 C 中调度或控制任意数量的线程。首先看下面的第一个线程。在这里它锁定了互斥锁1(以便其他线程无法访问代码)开始执行(代码不只是添加cmets),最后在完成任务后等待cond1,同样第二个线程锁定互斥锁2,开始执行其业务逻辑并最终等待在条件 cond2 和第 3 个线程锁定互斥锁 lock3 时,开始执行其业务逻辑并最终在条件 cond3 上等待。我没有在这里添加任何业务逻辑,因为这只是一个示例。在注释部分,您可以添加将以并行模式执行的业务逻辑。假设 thread3 依赖于 thread1 的最终输出,该输出将被插入到表中,并且 thread3 将在创建最终结果之前读取该信息,并且 thread2 依赖于 thread3 的最终结果来生成其最终结果。因此 thread1 在将数据插入表后,通过条件变量向 thread3 发出信号以继续其最终过程。这意味着thread1控制thread3。由于 thread2 取决于 thread3 的最终结果,因此 thread3 控制 Thread2 的执行。在这里,我们可以允许线程 1 独立执行,因为它的操作不依赖于任何其他线程,但是例如线程控制,我们在这里控制所有线程,因此线程 1 被线程 2 控制。
为了开始控制过程,我们首先释放线程1。在主线程(即主函数,每个程序都有一个主线程,在 C/C++ 中,一旦内核将控制权传递给主方法/函数,这个主线程由操作系统自动创建)我们调用 pthread_cond_signal(&cond1);一旦从主线程调用此函数,在 cond1 上等待的 thread1 将被释放并开始进一步执行。一旦完成最终任务,它将调用 pthread_cond_signal(&cond3);现在在条件 cond3 上等待的线程,即线程 3 将被释放,它将开始执行它的最后阶段,并将调用 pthread_cond_signal(&cond2);它将释放在条件 cond2 上等待的线程,即在本例中为 thread2。这是我们可以在多线程环境中调度和控制线程执行的方式。
#include<pthread.h>
pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
pthread_cond_t cond3 = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lock3 = PTHREAD_MUTEX_INITIALIZER;
int TRUE = 1;
void * threadMethod1(void *arg)
{
printf("In thread1\n");
do{
pthread_mutex_lock(&lock1);
//Add your business logic(parallel execution codes) here
pthread_cond_wait(&cond1, &lock1);
printf("I am thread1 generating the final report and inserting into a table \n");
pthread_cond_signal(&cond3);/* Now allow 3rd thread to process */
pthread_mutex_unlock(&lock1);
}while(TRUE);
pthread_exit(NULL);
}
void * threadMethod2(void *arg)
{
printf("In thread2\n");
do
{
pthread_mutex_lock(&lock2);
//Add your business logic(parallel execution codes) here
pthread_cond_wait(&cond2, &lock2);
printf("I am thread2 generating the final report and inserting into a table \n");
pthread_cond_signal(&cond1);
pthread_mutex_unlock(&lock2);
}while(TRUE);
pthread_exit(NULL);
}
void * threadMethod3(void *arg)
{
printf("In thread3\n");
do
{
pthread_mutex_lock(&lock3);
//Add your business logic(parallel execution codes) here
pthread_cond_wait(&cond3, &lock3);
printf("I am thread3 generating the final report and inserting into a table \n");
pthread_cond_signal(&cond2);
pthread_mutex_unlock(&lock3);
}while(TRUE);
pthread_exit(NULL);
}
int main(void)
{
pthread_t tid1, tid2, tid3;
int i = 0;
printf("Before creating the threads\n");
if( pthread_create(&tid1, NULL, threadMethod1, NULL) != 0 )
printf("Failed to create thread1\n");
if( pthread_create(&tid2, NULL, threadMethod2, NULL) != 0 )
printf("Failed to create thread2\n");
if( pthread_create(&tid3, NULL, threadMethod3, NULL) != 0 )
printf("Failed to create thread3\n");
pthread_cond_signal(&cond1);/* Now allow first thread to process first */
sleep(1);
TRUE = 0;/* Stop all the thread */
sleep(3);
/* this is how we join thread before exit from a system */
/*
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
pthread_join(tid3,NULL);*/
exit(0);
}
【讨论】: