运行多个线程时 C pthread 分段错误

发布于 2024-12-07 03:17:02 字数 1010 浏览 0 评论 0原文

如果我将 nThreads 保持在 300 以下,则以下代码运行不会出现任何问题,但如果我输入 400,则会出现分段错误。我认为这与最大线程数有关,但我不确定如何允许更多线程,或者至少如何确定我可以运行的最大线程数。有什么想法吗?提前致谢

#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
#include <unistd.h>

void* thread(void* arg);

int counter=0;
pthread_mutex_t counterMutex = PTHREAD_MUTEX_INITIALIZER;

int main(){
    int nThreads = 0;
    printf("How many threads? ");
    scanf("%d", &nThreads);
    pthread_t* threads = (pthread_t*)malloc(nThreads*sizeof(pthread_t));

    for(int i=0; i < nThreads; i++){
        pthread_create(&threads[i], NULL, thread, (void*)&i);
    }
    for(int i=0; i < nThreads; i++){
       pthread_join(threads[i], NULL);
    }
    printf("counter is %d\n\n", counter);
    exit(0);
}

void* thread(void* arg){
    pthread_mutex_lock(&counterMutex);
    counter++;
    printf("thread %d, counter is %d\n\n", *(int*)arg, counter);
    pthread_mutex_unlock(&counterMutex);
    pthread_exit(NULL);
}

The following code is running without any problems if I keep nThreads under 300, but if I enter 400 for example, then I get a segmentation fault. I think this has to do with maximum number of threads, but I am not sure how to allow more threads, or at least how to determine the maximum number of threads I can run. any idea? thanks in advance

#include <stdlib.h>
#include <pthread.h>
#include <malloc.h>
#include <unistd.h>

void* thread(void* arg);

int counter=0;
pthread_mutex_t counterMutex = PTHREAD_MUTEX_INITIALIZER;

int main(){
    int nThreads = 0;
    printf("How many threads? ");
    scanf("%d", &nThreads);
    pthread_t* threads = (pthread_t*)malloc(nThreads*sizeof(pthread_t));

    for(int i=0; i < nThreads; i++){
        pthread_create(&threads[i], NULL, thread, (void*)&i);
    }
    for(int i=0; i < nThreads; i++){
       pthread_join(threads[i], NULL);
    }
    printf("counter is %d\n\n", counter);
    exit(0);
}

void* thread(void* arg){
    pthread_mutex_lock(&counterMutex);
    counter++;
    printf("thread %d, counter is %d\n\n", *(int*)arg, counter);
    pthread_mutex_unlock(&counterMutex);
    pthread_exit(NULL);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月寒剑心 2024-12-14 03:17:02

您不检查 pthread_create 是否成功或失败。如果失败,您最终会使用无效的 pthread_t 调用 pthread_join

You don't check if pthread_create succeeded or failed. If it fails, you wind up calling pthread_join with an invalid pthread_t.

暗地喜欢 2024-12-14 03:17:02

如果您创建那么多线程,那么您就做错了,除非您使用的是超级计算机。 1990 年代的方法是为每个“状态”(连接、任务等)创建一个线程,但当前(也是正确的)方法是仅创建与 CPU/核心(给予或接受)一样多的线程,然后使用异步完成其余的事件。

You're doing something wrong if you're creating that many threads unless you're on a supercomputer. The 1990s method was to create a thread for each "state" (connection, task, etc.) but the current (and correct) approach is to create only as many threads as CPUs/Cores (give or take), and then use asynchronous events to pull off the rest.

十年不长 2024-12-14 03:17:02

为什么你甚至需要 400 个线程?您是否意识到拥有大量额外同步的线程会极大地减慢您的程序速度?

Why do you even need 400 threads? Do you realise that having that huge number of threads which are additionally synchronised will slow down your program incredibly?

不如归去 2024-12-14 03:17:02

pthread_mutex_init 不会出错!并尝试错误条件

pthread_mutex_init would not go amiss! And try for the error conditions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文