簡體   English   中英

C Pthread:僅同時運行10個線程(這里的問題是什么)

[英]C Pthread: Running only 10 threads simultaneously (what is the problem here)

因此,我對C語言中pthread的整個概念還很陌生,但是請聽我說。 我有以下代碼:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>


static pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t   endCond = PTHREAD_COND_INITIALIZER;
static pthread_cond_t   startCond = PTHREAD_COND_INITIALIZER;

void * threadThingy(void * n){
    pthread_cond_wait(&startCond, &mutex);
    printf("%d: RAND: %d\n", *((int*)n), rand());

    //Lock mutex before broadcasting to main thread
    pthread_mutex_lock(&mutex);
    pthread_cond_broadcast(&endCond);
    pthread_mutex_unlock(&mutex);


    free(n);
    fflush(stdout);
    return 0;
}


int main(void){
    printf("Starting\n");
    pthread_t threads[100];
    int i = 0;

    while(i < 10){
        int *arg = malloc(sizeof(int));
        *arg = i;
        pthread_create(&threads[i], NULL, threadThingy, arg);
        i++;
    }

    pthread_mutex_lock(&mutex);
    pthread_cond_broadcast(&startCond);

    int finished = 0;

    while(finished <= 100){
        pthread_cond_wait(&endCond, &mutex);

        //Lock mutex so no other requests can come in
        pthread_mutex_lock(&mutex);
        finished++;

        int *arg = malloc(sizeof(int));
        *arg = 11;
        pthread_create(threads[i], NULL, threadThingy, arg);
        i++;
        pthread_cond_broadcast(&startCond);
        pthread_mutex_unlock(&mutex);

    }

    printf("Stopping\n");

    sleep(1000);
}

總體目標是在100個線程中同時運行(僅)10個線程。我的想法是啟動10個線程,而不是等到一個線程完成再啟動另一個線程。 因此,我讓程序等待線程返回,然后啟動一個新線程,以便替換剛剛返回的線程。 我錯過了什么? 因為現在我只將其作為輸出:

起始0:RAND:1804289383

正如Lavigne958所提到的,在threadThingy()函數中,由於pthread_cond_wait()會導致死鎖,它將導致死鎖。 同樣,您嘗試將其鎖定在下一行。 這導致死鎖。

有幾件事需要檢查:

  1. 您需要在調用pthread_cond_wait()之前鎖定互斥鎖。

  2. 如果您解決了上述問題,則將多個條件變量與相同的互斥鎖一起使用可能會導致進一步的死鎖。

  3. 如果不加入線程,最好使用PTHREAD_CREATE_DETACHED屬性創建分離的線程。

  4. 可以同時使用一個信號量或一個條件變量(和一個互斥量)來解決N個線程同時運行的問題。 下面是帶有信號燈的示例。

     #include <stdio.h> #include <pthread.h> #include <semaphore.h> #include <unistd.h> sem_t mysem; #define NUM_CONCURRENT_THREADS 4 #define MAX_THREADS 40 void *thread(void *arg) { printf("Thread id %ld: started\\n", pthread_self()); sleep(5); // Do some work printf("Thread id %ld: Exiting\\n", pthread_self()); sem_post(&mysem); return NULL; } int main() { pthread_t t[MAX_THREADS]; pthread_attr_t attr; int rc, i = 0; sem_init(&mysem, 0, NUM_CONCURRENT_THREADS); rc = pthread_attr_init(&attr); rc = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); printf("\\nParent begin\\n"); while(i < MAX_THREADS) { sem_wait(&mysem); pthread_create(&t[i], &attr, thread, NULL); i++; } printf("\\nParent end.\\n"); sem_destroy(&mysem); return 0; } 

請查看博客Tech Easy以獲取有關線程的更多信息。

在線程運行的功能中,您首先要等待某種條件,但之前忘記使用互斥鎖。 因此,您必須先使用互斥鎖,然后再等待該條件。

您有所謂的僵局。

發生的是:

  1. 第一個線程喚醒(pthread_con_wait函數已經為您獲取了鎖)
  2. 然后您嘗試再次獲取該鎖=>死鎖,因為您已經擁有該鎖,因此自己有點死鎖。

暫無
暫無

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

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