我创建了 10 个线程,想要以循环方式执行 3 次。最初全部 线程正在等待。主线程向线程 0 发送信号,收到信号后线程 0 被唤醒并 然后执行一些任务,然后向线程 1 发送信号,这会像 thread1-> thread2->thread3 一样重复 ....->线程9。然后线程 9-> 线程 0。
我正在尝试实现这个
线程 i 执行一些任务,然后向线程 (i+1) 发送信号,然后线程 i 进入休眠状态。线程 (i+1) 将在 t 秒后唤醒(意味着线程 i+1 唤醒时间 - 线程 i sleep 时间 = t 秒),线程 (i+1) 将执行一些任务,向线程 (i+2) 发送信号然后去 sleep ,这会重复几次(3)次。
虽然我能够从线程 0-> 线程 1 -> .... 线程 9 发送信号(循环仅执行一次),我无法发送信号线程 9 -> 线程 0并且这就是为什么我无法重复这个循环3次。
我在哪里犯了错误?
任何帮助将不胜感激。 我在 linux 内核 2.6.32 下使用 g++ 4.6.3。
这是我的预期输出
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 blocked\n",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblocked\n", 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 blocked\n",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblocked\n", 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 - %s\n", argv[0]);
printf("Create %d threads\n", NTHREADS);
for(i=0; i<NTHREADS; ++i) {
rc = pthread_create(&threadid[i], NULL, threadfunc, (void *)i);
printf("Thread created=%d\n", 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 cleanup\n");
for (i=0; i<NTHREADS; ++i) {
rc = pthread_join(threadid[i], NULL);
}
pthread_cond_destroy(&cond[0]);
pthread_mutex_destroy(&mutex);
printf("Main completed\n");
return 0;
}
最佳答案
引用pthread_wait后我自己解决了我的问题。
所有线程按循环顺序运行一次,然后程序终止/完成执行,因为没有线程等待下一轮,全部完成。
因此,要多次(3)运行相同的序列,需要正确修改等待变量,以便每个线程将等待下一轮直到第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 blocked\n",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblocked\n", 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 blocked\n",my_id);
rc = pthread_cond_wait(&cond[my_id], &mutex);
printf("Thread %d unblocked\n", 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++ - 在 c/c++ 中使用 pthread_cond_wait 和 pthread_cond_signal 以循环方式执行多个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22813364/