首页 > 解决方案 > 为什么来自 main 的 Pthread Signal 会挂起代码?

问题描述

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

  1. 使用 Pthread 从 main 创建单个线程
  2. 在线程内部,Mutex 被锁定,计数器计数值,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 的新手。

提前致谢。

标签: clinuxmultithreadingpthreads

解决方案


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_exitinc_xtaskDonecount_mutex


推荐阅读