【发布时间】:2011-03-24 04:46:34
【问题描述】:
我已经在一个使用 pthreads 的小型 C 程序上工作了几天。昨天我或多或少都在寻找死锁错误,但现在我发现问题并不是真正的死锁错误。下面这段代码有完全相同的问题。
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define NTHREADS 507
pthread_mutex_t runningThreadsMutex;
pthread_cond_t runningThreadsCond;
int runningThreads = 0;
void* HelloWorld(void* arg) {
sleep(1);
pthread_mutex_lock(&runningThreadsMutex);
runningThreads--;
printf("End thread %d\n", runningThreads);
pthread_cond_signal(&runningThreadsCond);
pthread_mutex_unlock(&runningThreadsMutex);
return NULL;
}
int main() {
pthread_t thread;
pthread_mutex_init(&runningThreadsMutex, NULL);
pthread_cond_init(&runningThreadsCond, NULL);
for (int i = 0; i < NTHREADS; ++i) {
pthread_mutex_lock(&runningThreadsMutex);
printf("Create thread %d\n", runningThreads++);
pthread_mutex_unlock(&runningThreadsMutex);
pthread_create(&thread, NULL, HelloWorld, NULL);
// pthread_detach(thread);
}
pthread_mutex_lock(&runningThreadsMutex);
while(runningThreads > 0) {
pthread_cond_wait(&runningThreadsCond, &runningThreadsMutex);
}
pthread_mutex_unlock(&runningThreadsMutex);
return 0;
}
对于 NTHREADS
Create thread 0
Create thread 1
.
.
.
Create thread 505
End thread 505
End thread 504
.
.
.
End thread 0
并按其应有的方式终止。但是,如果我使用 NTHREADS >= 506,例如NTHREADS = 510 我明白了
Create thread 0
Create thread 1
.
.
.
Create thread 509
End thread 509
End thread 508
.
.
.
End thread 4
它在没有终止的情况下停止。所以看起来最后四个(510-506 = 4)线程永远不会终止(或根本不会启动?)。
我也在一台旧的 32 位 linux 机器上尝试了这段代码。在那里我得到了相同的行为,除了它适用于 NTHREADS = 382(而不是 506)。
当我在谷歌上搜索解决方案时,我还发现了这个问题:http://bytes.com/topic/c/answers/728087-pthreads-limit,有人在使用 pthread_join 时遇到了同样的问题(使用 pthreads 时可能更自然),但他们没有给出任何好的解释。
谁能向我解释我做错了什么以及这段代码的根本问题是什么?我想这一定是对允许线程数的某种限制,但是我应该如何处理呢?
【问题讨论】:
-
保证您能够运行 64 个线程(具有默认属性)。除此之外,这是实施的礼物。