c - 为什么来自 main 的 Pthread Signal 会挂起代码?
问题描述
使用 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 的新手。
提前致谢。
解决方案
count_mutex
并且count_threshold_cv
没有初始化,添加:
int main()
{
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init(&count_threshold_cv, NULL);
//...
E1_ptr
未初始化。有很多方法可以解决它:
您可以调用malloc
分配内存:
struct example *E1_ptr = malloc(sizeof(struct example));
E1_ptr->a = 0;
E1_ptr->i = 0;
或持有指向局部变量的指针:
struct example ex;
struct example *E1_ptr = &ex; //malloc(sizeof(struct example));
E1_ptr->a = 0;
E1_ptr->i = 0;
或者
struct example ex;
ex.a = 0;
ex.i = 0;
然后创建线程pthread_create(&inc_x_thread, NULL, inc_x, &ex)
pthread_cond_signal
功能不等待。如果一个线程被条件变量pthread_cond_signal
函数阻塞,则解除对该线程的阻塞,否则立即返回,不等待,什么也不做。因此,您的 for 循环会尽快执行 10 次迭代,而无需pthread_cond_wait
调用任何等待。所以可以将你的for循环重写为无限循环,pthread_cond_signal
反复调用。
if(pthread_create(&inc_x_thread, NULL, inc_x, E1_ptr)) {
printf("Error creating thread\n");
}
while(1) { // INFINITE LOOP
if(pthread_cond_signal(&count_threshold_cv)) {
printf("Error Signaling thread at sample = %d\n",i);
}
if (taskDone) // testing global flag, if 1 break
break; // it means inc_x thread has ended
}
if(pthread_join(inc_x_thread, NULL)) { // it was pointed out in comment
printf("Error joining thread\n"); // you need to join at the end of main function
}
taskDone
是全局int,默认值为 0。它设置为在函数中调用1
之前。设置/检查应该包含一些同步机制,例如通过添加新的互斥锁或使用。pthread_exit
inc_x
taskDone
count_mutex
推荐阅读
- xml - 如何使用 TimeBasedTriggeringPolicy 从 Storm 中删除旧的日志文件?
- php - 如何在 laravel 5.7 中对数组进行排序
- r - 读取具有不同部分的复杂 CSV 的问题
- c# - 为什么只有当我关注数据出现的行时,datagridview 才显示数据?
- reactjs - 迁移到 material-ui v4,我收到 Modal 组件的警告
- matlab - 如何从 MATLAB 上的元胞数组访问特定文件扩展名的文件
- json - Athena 在另一个 json 结构数组中未嵌套 json 字符串数组
- python - 基于逻辑非从现有数据框创建新数据框
- python - 是否可以使用 hive 为 python 文件安排 CRON 任务
- php - 如何让 PHP 通过我在 GCP 上托管的网站运行,以允许基于浏览器的文件上传到我的托管空间?