c - 如果我们需要按特定顺序打印多线程计算的结果,为什么使用信号而不是广播?
问题描述
在下面的代码中,我创建了一个线程池,它们在一个公共数组上进行一些计算。我希望计算并行完成,但是,我想打印计算结果,以便线程 id x 不会在线程 id y 之前打印其结果,其中 x > y。所以首先我想打印 id 0 的结果,然后是 1、2 等。
我正在使用 pthreads 来执行此操作。最初我曾经pthread_cond_broadcast
唤醒阻塞的线程(一切正常),但后来我pthread_cond_signal
只是出于好奇而尝试。有趣的是,该程序仍然可以正常工作。
但我不明白为什么。那是:
- 我产生,说5个线程。他们都完成了他们的计算。
- 其中 4 个被阻塞,而线程 id 0 打印其结果和信号。
- 根据规范
pthread_cond_signal
“应至少解除一个被阻塞的线程”。所以也许它解除了线程 id 3 的阻塞。 - 线程 id 3 无法继续,因为还没有轮到它打印结果,所以它等待。
- 出现僵局。
那么为什么pthread_cond_signal
仍然有效呢?是因为反复的运气还是因为我的操作系统创建了一个阻塞线程队列,而线程 id 1 恰好位于该队列的头部?
这是我的代码(等待/信号逻辑在函数中ComputeThread
):
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#define DEFAULT_VAL 5
#define ARR_LEN 5
#define THREADS_NUM 5
typedef struct helper_t {
int * currId;
int threadId;
int * computeArr;
pthread_mutex_t * mutex;
pthread_cond_t * cond;
} helper_t;
int * initComputeArr(int len) {
int i;
int * computeArr = (int *) malloc(sizeof(int) * len);
for(i = 0; i < len; i++) {
computeArr[i] = DEFAULT_VAL;
}
return computeArr;
}
void mallocError() {
printf("malloc error\n");
exit(EXIT_FAILURE);
}
helper_t * initHelpers(pthread_mutex_t * mutex, pthread_cond_t * cond, int * computeArr) {
int i;
helper_t * helpers = (helper_t *) malloc(sizeof(helper_t) * THREADS_NUM);
int * currId = (int *) malloc(sizeof(int));
if(!helpers || !currId) {
mallocError();
} else {
*currId = 0;
for(i = 0; i < THREADS_NUM; i++) {
helpers[i].mutex = mutex;
helpers[i].cond = cond;
helpers[i].computeArr = computeArr;
helpers[i].currId = currId;
helpers[i].threadId = i;
}
}
return helpers;
}
void printHelper(helper_t * h) {
printf("threadId %d, currId %d\n", h->threadId, *h->currId);
}
void printHelpers(helper_t * h, int len) {
int i;
for(i = 0; i < len; i++) {
printHelper(&h[i]);
}
}
int calc(int * arr, int uptoIndex) {
int i, sum = 0;
for(i = 0; i <= uptoIndex; i++) {
sum += arr[i];
}
return sum;
}
void * ComputeThread(void * arg) {
int calcResult;
helper_t * h = (helper_t *) arg;
pthread_mutex_t * mutex = h->mutex;
pthread_cond_t * cond = h->cond;
calcResult = calc(h->computeArr, h->threadId);
sleep(1);
pthread_mutex_lock(mutex);
while(*h->currId != h->threadId) {
printf("id %d waiting...\n", h->threadId);
pthread_cond_wait(cond, mutex);
}
printf("curr %d, threadId %d, result %d\n", *h->currId, h->threadId, calcResult);
(*h->currId)++;
pthread_cond_signal(cond);
pthread_mutex_unlock(mutex);
pthread_exit((void *) calcResult);
}
int main() {
int i;
pthread_mutex_t mutex;
pthread_cond_t cond;
int * computeArr;
int * calcResutls;
helper_t * helpers;
pthread_t threads[THREADS_NUM];
computeArr = initComputeArr(ARR_LEN);
calcResutls = initComputeArr(ARR_LEN);
helpers = initHelpers(&mutex, &cond, computeArr);
printHelpers(helpers, THREADS_NUM);
for(i = 0; i < THREADS_NUM; i++) {
pthread_create(&threads[i], NULL, ComputeThread, (void *) &helpers[i]);
}
for(i = 0; i < THREADS_NUM; i++) {
pthread_join(threads[i], (void **) &calcResutls[i]);
}
for(i = 0; i < ARR_LEN; i++) {
printf("%d, ", calcResutls[i]);
}
printf("\n");
printf("end of calc\n");
return 0;
}
解决方案
我看到未定义的行为被调用,因为代码未初始化互斥锁和条件。
推荐阅读
- java - vsphere-automation-sdk-java 未经身份验证-vSphere 6.5
- c# - 如何合并两个可观察对象并提前完成
- php - 哪个是从数据库中搜索数百万数据的最佳搜索引擎软件?
- facebook-graph-api - 为什么 Facebook 登录在模拟器和实体手机之间的行为不同
- python - 熊猫列过滤器慢
- javascript - 如何使用带有 if 条件的 JavaScript 更改背景图像?
- python - 具有多个列表和条件的排列
- python - 纹理图像匹配的算法建议
- python - python中的单个var赋值和多个var赋值之间有什么不同的行为吗?
- c++ - 我的 KMP 算法有问题吗?