僵局的原因在哪里?
我打算让两个线程等待来自第三个线程的信号。
这两个线程执行相同的工作,但一次只有一个线程获取信号。一旦满足特定条件(捕获的信号数量),它们就会自行终止。
然后最终主线程取消了第三个线程。
我遇到了僵局,但无法弄清楚问题出在哪里。
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int n = 0;
int count = 0;
void* func1(void *ptr)
{
while(1)
{
pthread_mutex_lock(&mutex);
// wait for func3 to signal
pthread_cond_wait(&cond, &mutex);
count++;
if(count > 10)
{
printf("number %d terminate func1\n", n);
return (NULL);
}
else
{
printf("func1 got number:%d\n", n);
}
pthread_mutex_unlock(&mutex);
}
}
void* func2(void *ptr)
{
while(1)
{
pthread_mutex_lock(&mutex);
// wait for func3 to signal
pthread_cond_wait(&cond, &mutex);
count++;
if(count > 10)
{
printf("number %d terminate func2\n", n);
return (NULL);
}
else
{
printf("func2 got number:%d\n", n);
}
pthread_mutex_unlock(&mutex);
}
}
void* func3(void *ptr)
{
while(1)
{
pthread_mutex_lock(&mutex);
n++;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, char *argv[])
{
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, func1, NULL);
pthread_create(&t2, NULL, func2, NULL);
pthread_create(&t3, NULL, func3, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_cancel(t3);
return 0;
}
I intent to have two threads waiting for signals from a third thread.
These two threads do the same work but only one of them gets the signal at a time. Once a certain condition is meet(number of signal captured), they terminate themselves.
Then in the end the main thread cancel the third thread.
I got deadlock, but could not figure out where is the problem.
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int n = 0;
int count = 0;
void* func1(void *ptr)
{
while(1)
{
pthread_mutex_lock(&mutex);
// wait for func3 to signal
pthread_cond_wait(&cond, &mutex);
count++;
if(count > 10)
{
printf("number %d terminate func1\n", n);
return (NULL);
}
else
{
printf("func1 got number:%d\n", n);
}
pthread_mutex_unlock(&mutex);
}
}
void* func2(void *ptr)
{
while(1)
{
pthread_mutex_lock(&mutex);
// wait for func3 to signal
pthread_cond_wait(&cond, &mutex);
count++;
if(count > 10)
{
printf("number %d terminate func2\n", n);
return (NULL);
}
else
{
printf("func2 got number:%d\n", n);
}
pthread_mutex_unlock(&mutex);
}
}
void* func3(void *ptr)
{
while(1)
{
pthread_mutex_lock(&mutex);
n++;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
int main(int argc, char *argv[])
{
pthread_t t1, t2, t3;
pthread_create(&t1, NULL, func1, NULL);
pthread_create(&t2, NULL, func2, NULL);
pthread_create(&t3, NULL, func3, NULL);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_cancel(t3);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当
func1
或func2
退出时,您并未解锁互斥锁。You're not unlocking the mutex when
func1
orfunc2
exits.我认为问题是(正如 pst 指出的)是
return (NULL);
在您的func1
和func2
函数中;因为pthread_cond_wait(3posix)
返回时互斥体已锁定,因此当它们退出时,互斥体将保持锁定状态:尝试在
return (NULL);
之前解锁互斥体。I think the problem is (as pst points out) is the
return (NULL);
in yourfunc1
andfunc2
functions; becausepthread_cond_wait(3posix)
returns with the mutex locked, when they exit, the mutex is left locked:Try unlocking your mutex before
return (NULL);
.