运行多个线程时 C pthread 分段错误
如果我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不检查
pthread_create
是否成功或失败。如果失败,您最终会使用无效的pthread_t
调用pthread_join
。You don't check if
pthread_create
succeeded or failed. If it fails, you wind up callingpthread_join
with an invalidpthread_t
.如果您创建那么多线程,那么您就做错了,除非您使用的是超级计算机。 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.
为什么你甚至需要 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?
pthread_mutex_init
不会出错!并尝试错误条件pthread_mutex_init
would not go amiss! And try for the error conditions