为什么Pthread Signal从主挂起代码?

时间:2018-11-10 12:53:46

标签: 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循环的第1次迭代后停止线程。 信号是由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的新手。

谢谢。

1 个答案:

答案 0 :(得分:0)

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。在1函数中调用pthread_exit之前将其设置为inc_x。设置/检查taskDone应该带有某种同步机制,例如添加新的互斥锁或使用count_mutex