簡體   English   中英

為什么來自 main 的 Pthread Signal 會掛起代碼?

[英]why does Pthread Signal from main, hangs the code?

下面給出了使用 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 的新手。

提前致謝。

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阻塞,則函數解除阻塞該線程,否則立即返回而不等待並且什么都不做。 因此,您的 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。 inc_x函數中調用pthread_exit之前,它被設置為1 設置/檢查taskDone應該用一些同步機制包裝,例如通過添加新的互斥鎖或使用count_mutex

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM