【问题标题】:why does Pthread Signal from main, hangs the code?为什么来自 main 的 Pthread Signal 会挂起代码?
【发布时间】:2018-11-10 12:53:45
【问题描述】:

使用 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 的新手。

提前致谢。

【问题讨论】:

  • 这是因为您在发出信号之前就加入了。加入将等到线程完成。线程将等到您发出信号。这是一个死锁。将 join 移近程序末尾。
  • 感谢 serge 的建议,但之后程序也有相同的行为。它在第一次迭代后挂起。从主线程发出的线程信号是否正常。

标签: c linux multithreading pthreads


【解决方案1】:

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(&amp;inc_x_thread, NULL, inc_x, &amp;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。在pthread_exitinc_x 函数中调用之前,它被设置为1。设置/检查taskDone 应该包含一些同步机制,例如通过添加新的互斥锁或使用count_mutex

【讨论】:

  • 感谢您的回答。我假设在 for 循环的每次迭代中,我都将调用线程并完成其执行,直到执行 for 循环的另一次迭代。试图实现这样的目标
  • 有没有办法可以一次又一次地调用同一个线程?还是在 for 循环的每次迭代中,我都会创建一个新线程?
  • 如果线程已启动,您有 2 个选项:加入该线程或分离它。您不能再次调用同一个线程,您需要创建一个新线程。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-31
  • 2012-01-16
相关资源
最近更新 更多