【发布时间】:2018-11-10 12:53:45
【问题描述】:
使用 pthread 创建示例应用程序的要求如下:
- 使用 Pthread 从 main 创建单个线程
- 在线程内部,Mutex 被锁定,计数器计数值,while 循环递增,While 设置为最大计数 10。
- while 循环结束后,互斥锁被解锁。
上述要求我已经尝试使用 pthread 实现
代码如下:
#include <pthread.h>
#include <stdio.h>
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv;
int samples = 10;
int count = 0;
struct example
{
int i;
int a;
};
void *inc_x(void *x_void_ptr)
{
pthread_mutex_lock(&count_mutex);
printf("Thread is locked \n");
while(count < samples)
{
printf("inside While loop \n");
struct example *E2_ptr;
E2_ptr = (struct example *)x_void_ptr;
printf("inside thread count = %d\n",count);
E2_ptr->a = count;
E2_ptr->i = (count + 1);
count ++;
//pthread_cond_wait(&count_threshold_cv, &count_mutex);
}
pthread_mutex_unlock(&count_mutex);
printf ( "\n Test Successful for Thread\n");
pthread_exit(NULL);
}
int main()
{
int x = 100, y = 0,i = 0;
struct example *E1_ptr;
E1_ptr->a = 0;
E1_ptr->i = 0;
printf("Before\t E1_ptr->a = %d\t, E1_ptr->i = %d\n",E1_ptr->a,E1_ptr->i);
pthread_t inc_x_thread;
if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr))
{
printf("Error creating thread\n");
}
if(pthread_join(inc_x_thread, NULL))
{
printf("Error joining thread\n");
}
for(i = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %d\n",i);
}
}
printf("after\t E1_ptr->a = %d\t, E1_ptr->i = %d\n",E1_ptr->a,E1_ptr->i);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit (NULL);
return 0;
}
怀疑:
在上面的代码中,线程正确地执行了它的函数并退出了。 一旦条件被应用,即下面显示的代码被取消注释,
pthread_cond_wait(&count_threshold_cv, &count_mutex);
然后线程按预期在第一次 while 循环迭代后停止。 信号由 main 生成,代码如下:
for(i = 0; i<(samples-1); i++)
{
if(pthread_cond_signal(&count_threshold_cv))
{
printf("Error Signaling thread at sample = %d\n",i);
}
}
观察到信号从未发送。
有人可以指导我,我哪里出错了。我是 Pthreads 的新手。
提前致谢。
【问题讨论】:
-
这是因为您在发出信号之前就加入了。加入将等到线程完成。线程将等到您发出信号。这是一个死锁。将 join 移近程序末尾。
-
感谢 serge 的建议,但之后程序也有相同的行为。它在第一次迭代后挂起。从主线程发出的线程信号是否正常。
标签: c linux multithreading pthreads