【发布时间】:2016-11-11 18:04:31
【问题描述】:
我正在编写一个程序来测试我对条件变量的理解。基本上,线程 0 检查 count 是否为偶数,如果是,则增加它。如果不是,那么它会向线程 1 发出信号,该线程 1 会增加 count 变量。该过程一直持续到计数达到 15。这是我的代码:
#include <pthread.h>
#include <stdio.h>
#define numThreads 2
int count=0;
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;
void *checkEven(void *threadId)
{ while(count<=15){
//lock the mutex
pthread_mutex_lock(&count_mutex);
printf("even_thread: thread_id=%d count=%d\n",threadId,count);
if(count%2==0){
count++;
}
else{
printf("Odd count found, signalling to odd thread\n");
pthread_cond_signal(&count_threshold_cv);
}
pthread_mutex_unlock(&count_mutex);
sleep(1);
}
}
void *checkOdd(void *threadId)
{
pthread_mutex_lock(&count_mutex); //obtain a lock
while(count<=15){
pthread_cond_wait(&count_threshold_cv, &count_mutex); //wait() relinquishes the lock
count++;
printf("odd_thread: thread_id=%d, count=%d\n",threadId,count);
}
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}
int main()
{
pthread_t threads[numThreads];
int rc;
int a=0;
int b=0;
pthread_create(&threads[0], NULL, checkEven, (void *)a);
pthread_create(&threads[1], NULL, checkEven, (void *)b);
pthread_join(0,NULL);
pthread_join(1,NULL);
pthread_exit(NULL);
}
有人能告诉我为什么会出现分段错误(核心转储)错误吗?我知道当一个进程试图违反其他进程的地址空间时会发生此错误,但除此之外没有其他内容。有人可以帮忙吗?谢谢!
【问题讨论】:
-
问题是,一旦计数为奇数,偶数线程就会调用信号。因此,奇数线程获得锁并在 wait() 调用后继续,并增加计数。然后解锁,偶数线程获得锁,循环继续。
-
你没有正确初始化你的
pthread_mutex_t和pthread_cond_t。 -
我认为这不是它所面临问题的原因。实际上,我通过传递 0 而不是threads[0],弄乱了对join() 的调用。
-
我从未说过这是您观察到的行为的原因。我只是指出您的代码在这方面是不正确的。
-
感谢@EOF。你能告诉我它有什么问题吗?即使从我开始,我也一直像这样初始化它们。我什么时候错了?