使用 C/C++ 中的 pthread_cond_wait 和 pthread_cond_signal 以轮循机制方式执
Executing multiple threads in a round robin fashion using pthread_cond_wait and pthread_cond_signal in c/c++
我已经创建了 10 个线程,并希望以循环方式执行 3 次。最初全部线程正在等待。主线程向线程 0 发送信号,在接收信号时线程 0 唤醒并然后执行一些任务,然后将信号发送到线程 1,这像线程 1->线程 2->线程 3 一样重复....->线程9.然后线程 9->线程 0。
我正在尝试实现这个
线程 (I+1) 将在 T 秒后唤醒(表示线程 I+1 唤醒时间 - 线程 I 睡眠时间 = t 秒),线程 (i+1) 将执行一些任务,将信号发送到线程 (i+2) 并进入睡眠状态,这将重复几次 (3) 次。
虽然我能够从线程 0->线程 1 发送信号 ->....线程 9(循环仅执行一次),我无法发送信号线程 9 ->线程 0,这就是为什么我无法重复此循环 3 次的原因。
我哪里犯了错误?
任何帮助将不胜感激。我在 linux 内核 4.6.3.3 下使用 g++ 2.6.32。
这是我的预期输出
Create 5 threads
Thread created=0
Thread created=1
Thread created=2
Thread created=3
Thread created=4
Thread 4 blocked
Thread 3 blocked
Thread 2 blocked
Thread 1 blocked
Thread 0 blocked
Wake up all waiting threads...
Thread 0 unblocked
Thread 1 unblocked
Thread 2 unblocked
Thread 3 unblocked
Thread 4 unblocked
Thread 4 blocked // repeataion of same sequence
Thread 3 blocked
Thread 2 blocked
Thread 1 blocked
Thread 0 blocked
Thread 0 unblocked
Thread 1 unblocked
Thread 2 unblocked
Thread 3 unblocked
Thread 4 unblocked
Wait for threads and cleanup
Main completed
这是我的实际输出
Create 5 threads
Thread created=0
Thread created=1
Thread created=2
Thread created=3
Thread created=4
Thread 4 blocked
Thread 3 blocked
Thread 2 blocked
Thread 1 blocked
Thread 0 blocked
Wake up all waiting threads...
Thread 0 unblocked
Thread 1 unblocked
Thread 2 unblocked
Thread 3 unblocked
Thread 4 unblocked
Wait for threads and cleanup
Main completed
这是我的代码
#include <pthread.h>
#include <iostream>
#include <stdio.h>
/* For safe condition variable usage, must use a boolean predicate and */
/* a mutex with the condition. */
int conditionMet = 0;
pthread_cond_t cond[5];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define NTHREADS 5
void *threadfunc(void *parm)
{
int i;
long my_id = (long)parm;
int rc;
// Initially all threads will wait
rc = pthread_mutex_lock(&mutex);
printf("Thread %d blockedn",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblockedn", my_id);
rc = pthread_mutex_unlock(&mutex);
int count=0;
while(count++<3) // This line makes no sense, no repeatation as expected.
{
rc = pthread_mutex_lock(&mutex);
while (!conditionMet) {
printf("Thread %d blockedn",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblockedn", my_id);
}
rc = pthread_mutex_unlock(&mutex);
// sending signal to next thread i+1
rc = pthread_mutex_lock(&mutex);
rc = pthread_cond_signal(&cond[(my_id+1)%NTHREADS]);
rc = pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc, char **argv)
{
int rc=0;
int i;
pthread_t threadid[NTHREADS];
for(rc=0;rc<NTHREADS;rc++)
cond[rc]= PTHREAD_COND_INITIALIZER;
printf("Enter Testcase - %sn", argv[0]);
printf("Create %d threadsn", NTHREADS);
for(i=0; i<NTHREADS; ++i) {
rc = pthread_create(&threadid[i], NULL, threadfunc, (void *)i);
printf("Thread created=%dn", i);
}
sleep(5); /* Sleep is not a very robust way to serialize threads */
rc = pthread_mutex_lock(&mutex);
/* The condition has occured. Set the flag and wake up any waiting threads */
conditionMet = 1;
printf("Wake up all waiting threads...n");
rc = pthread_cond_signal(&cond[0]);
rc = pthread_mutex_unlock(&mutex);
printf("Wait for threads and cleanupn");
for (i=0; i<NTHREADS; ++i) {
rc = pthread_join(threadid[i], NULL);
}
pthread_cond_destroy(&cond[0]);
pthread_mutex_destroy(&mutex);
printf("Main completedn");
return 0;
}
在参考pthread_wait之后,我自己解决了我的问题。
所有线程以循环顺序运行一次,然后程序终止/完成执行,因为没有线程正在等待下一轮,全部完成。
因此,要多次运行相同的序列,需要正确修改等待变量,以便每个线程将等待下一轮直到 3 次执行。
这是修改后的程序:
void *threadfunc(void *parm)
{
int i;
long my_id = (long)parm;
int rc;
/*
DELETE THIS PORTION, OTHERWISE NO OUTPUT, AGAIN HANGED BEFORE SINGLE SEQUENCE.
IT WILL NOT ENTER INSIDE LOOP while(count++<3)
// Initially all threads will wait
rc = pthread_mutex_lock(&mutex);
printf("Thread %d blockedn",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblockedn", my_id);
rc = pthread_mutex_unlock(&mutex);
*/
int count=0;
while(count++<3)
{
rc = pthread_mutex_lock(&mutex);
while ( conditionMet != my_id)
{
printf("Thread %d blockedn",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblockedn", my_id);
}
rc = pthread_mutex_unlock(&mutex);
rc = pthread_mutex_lock(&mutex);
conditionMet = (my_id+1)%NTHREADS ; // This is important to wait for next time
rc = pthread_cond_signal(&cond[(my_id+1)%NTHREADS]);
rc = pthread_mutex_unlock(&mutex);
}
return NULL;
}
相关文章:
- 在 c++11 中为 pthread 设置调度参数
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- pthread只有在线程数量较少时才可以正常工作
- pthread执行时间比顺序执行时间差
- 使用 -pthread 如何不违反 ODR 规则?
- 使用 pthread 的多线程
- 销毁 pthread 互斥体和 C++ 中的取消初始化顺序
- 函数 AfxEndThread 中的读取访问冲突"pThread-> was nullptr"
- 锁定步进pthread互斥
- Pthread段错误,使用指向main中变量的指针
- pthread导致的内存泄漏
- GCC:--静态链接到pthread的整个存档配方在最近的GCC版本中停止工作
- 正确地编写一个类,并将pthread与vlc库和c++一起使用
- 直接读取 pthread 互斥锁的所有者字段是否安全?
- 为什么 pthread 会减慢代码速度?
- 对 pthread CLion 的未定义引用
- C++ 17:pthread的生成文件标志
- 将带有结构的参数传递给 pthread
- Posix 线程类和启动例程 (pthread)
- 在 pthread 中使用共享变量