gpt4 book ai didi

C P线程: Running only 10 threads simultaneously (what is the problem here)

转载 作者:行者123 更新时间:2023-11-30 14:46:08 27 4
gpt4 key购买 nike

所以我对 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 个线程,而不是等到一个线程完成后再启动另一个线程。因此,我让程序等待,直到线程返回,然后启动一个新线程,以便替换刚刚返回的线程。我错过了什么?因为现在我只得到这个作为输出:

Starting 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有关线程的更多信息。

关于C P线程: Running only 10 threads simultaneously (what is the problem here),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526346/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com