你好我正嘗試使用條件變量同步分離的線程,但是我發現了一個有時會導致內存泄漏(取決於調度程序心情)的錯誤。我認爲這個代碼是自我解釋的。我將不勝感激任何建議。POSIX線程 - 使用條件變量同步DETACHED線程MEMORY LEAK
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
using namespace std;
struct TThrArg
{
pthread_t m_ID;
bool m_IsRunning;
};
TThrArg g_Threads[64];
int g_Counter;
pthread_mutex_t g_Mtx;
pthread_cond_t g_Cond;
void * thrFunc (void * arg)
{
TThrArg * data = (TThrArg *) arg;
// do some stuff
// -----------------------------------
// for (int i = 0; i < 5000; ++i)
// for (int j = 0; j < 5000; ++j)
// int x = 0;
// printf("Thread: %lu running...\n", data->m_ID);
// -----------------------------------
pthread_mutex_lock(&g_Mtx);
memset(data, 0, sizeof(TThrArg));
--g_Counter;
pthread_cond_signal(&g_Cond);
pthread_mutex_unlock(&g_Mtx);
sleep(1); // --> this spot causes that main may end before return NULL so resources will not be freed
return NULL;
}
void createThread (void)
{
pthread_mutex_lock(&g_Mtx);
for (int i = 0; i < 64; ++i)
{
if (g_Threads[i].m_IsRunning == 0)
{
g_Threads[i].m_IsRunning = 1;
++g_Counter;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&g_Threads[i].m_ID, &attr, thrFunc, &g_Threads[i]);
pthread_attr_destroy(&attr);
break;
}
}
pthread_mutex_unlock(&g_Mtx);
}
int main (int argc, char * argv[])
{
pthread_mutex_init(&g_Mtx, NULL);
pthread_cond_init(&g_Cond, NULL);
g_Counter = 0;
for (int i = 0; i < 64; ++i)
createThread();
pthread_mutex_lock(&g_Mtx);
while (g_Counter != 0)
{
pthread_cond_wait(&g_Cond, &g_Mtx);
}
pthread_mutex_unlock(&g_Mtx);
pthread_mutex_destroy(&g_Mtx);
pthread_cond_destroy(&g_Cond);
return 0;
}
感謝您的信息,但是有沒有「乾淨」的方式來防止泄漏,只需編輯上面的代碼? – guderkar
沒有。知道一個線程使用的所有資源的唯一方法就是加入它。在線程中執行的最後一個動作與線程實際終止並釋放其所有資源之間始終存在爭用條件。 –
非常感謝您的先生。 – guderkar