c - 为什么来自 main 的 Pthread 信号会挂起代码?

标签 c linux multithreading pthreads

使用 pthread 创建示例应用程序的要求如下:

  1. 使用 Pthread 从 main 创建单个线程
  2. 在线程内部,互斥锁被锁定,计数器对值进行计数,同时 while 循环递增,While 设置为最大计数 10。
  3. 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_mutexcount_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 函数会解除对该线程的阻塞,否则立即返回而不等待并且不执行任何操作。因此,具有 10 次迭代的 for 循环会尽快执行,而无需等待 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。在 inc_x 函数中调用 pthread_exit 之前,它被设置为 1。设置/检查 taskDone 应该用某种同步机制包装,例如通过添加新的互斥体或使用 count_mutex

关于c - 为什么来自 main 的 Pthread 信号会挂起代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53239149/

相关文章:

c - 局部变量在栈上的分配顺序

c - 如何从函数中的数组返回结构? (指针)

c++ - 防止简单的宏替换

linux - 如何修复不重新启动计算机的备份脚本

linux - Raspberry Pi 系统在文件传输时崩溃

php - 有人可以将它移植到 C 吗?

python - Pyserial:无法配置端口:(5,'输入/输出错误)

java - 关于实现 Runnable 的问题

java - 为什么在字段变量上同步并在同步块(synchronized block)内递增它会导致打印乱序?

java - Android SurfaceView当前线程引用