メインスレッドで使用し、後で子スレッドでpthread_create()
使用しているアプリケーションがあります。pthread_detach()
pthread_exit()
約 54pthread_create()
回の呼び出しの後、それぞれが後続の呼び出しとペアにpthread_detach()
なりpthread_exit()
、pthread_create()
失敗します。ENOMEM
「メモリ不足」の失敗です。
pthread_exit()
古いスレッドのメモリが解放されず、アプリケーションでメモリ リークが発生し、最終的に不足する原因は何ですか?
これは Linux Centos 5 64 ビットで実行されていますが、32 ビットで構築されたアプリケーションです。
pthread_create()
と の両方を呼び出すスレッドを作成するコードを次に示しますpthread_detach()
。
int
_createThread()
{
pthread_attr_t attr;
int return_val;
return_val = setupMutex(_Mtx());
if (return_val != 0) {
return return_val;
}
return_val = setupCond(_StartCond());
if (return_val != 0) {
return return_val;
}
return_val = setupCond(_EndCond());
if (return_val != 0) {
return return_val;
}
return_val = pthread_attr_init(&attr);
if (return_val != 0) {
return -1;
}
size_t stackSize = 1024 * 1024 * 64; // Our default stack size 64MB.
return_val = pthread_attr_setstacksize(&attr, stackSize);
if (return_val != 0) {
return -1;
}
int tries = 0;
retry:
// _initialize() gets called by the thread once it is created.
return_val = pthread_create(&_threadId, &attr,
(void *(*)(void *))_initialize,
(void *)this);
if (return_val != 0) {
if (return_val == EAGAIN) {
if (++tries < 10) {
Exit::deferredWarning(Exit::eagainThread);
goto retry;
}
}
return -1;
}
return_val = pthread_attr_destroy(&attr);
if (return_val != 0) {
return -1;
}
return_val = pthread_detach(_threadId);
if (return_val != 0) {
return -1;
}
// Wait for the new thread to finish starting up.
return_val = waitOnCond(_Mtx(), _EndCond(), &_endCount, 10 /* timeout */, 0,
"_createThread-end");
if (return_val != 0) {
return -1;
}
return 0;
}
void
_exitThread()
{
(void) releaseCond(_Mtx(), _EndCond(), &_endCount, "_exitThread-end");
pthread_exit(NULL);
}