pthread 在完成后泄漏内存
pthread leaks memory after it finishes
我知道通过调用 pthread_join
或 pthread_detach
将在线程完成后释放线程使用的资源,但我的情况并不那么容易。
首先我希望能够从父线程中终止子线程,所以我写了这样的东西:(动态分配的buf在那里显示我似乎找不到使用方法pthread_detach
因为我不知道如何等待子线程完成(所以我可以释放 buf)如果我分离它)
bool running = true;
void *foo(void *buf)
{
while (running)
{
// Do something with buf
}
return NULL;
}
int main(int argc, char **argv)
{
char *buf = new char[1024];
pthread_t tid;
pthread_create(&tid, NULL, foo, buf);
string cmd;
while (true)
{
cin >> cmd;
if (!cmd.compare("stop"))
{
running = false;
pthread_join(tid, NULL);
delete[] buf;
}
else
{
// Do something
}
}
return 0;
}
这似乎有效。但是子线程有时会在父线程想要终止它之前完成。在这种情况下,父线程被阻塞所以
如何通知父线程子线程已经终止,以便父线程可以调用pthread_join
?
有没有一种方法可以使用 pthread_detach
并且仍然能够等待子线程完成以便之后我可以释放 buf(虽然看起来我可以释放子线程中的 buf线程在这个演示中,这对我的实际应用来说是不可能的)?
- How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
有各种各样的选择。以下是三个相当有希望的:
您可以让子线程设置一个全局标志变量。
您可以让子线程用 pthread_kill()
向父线程发送信号。
您可以设置一个管道,让子进程在完成后写入,并让父进程从中执行非阻塞读取以测试子进程是否完成。
- Is there a way to use pthread_detach and still be able to wait for the child thread to finish so I can free buf afterwards (though it
seems I can free buf in the child thread in this demo, it's impossible
for my real application)?
没有。一旦一个线程被分离,它永远不能被加入。
您可以考虑让线程管理自己的资源。或者,如果主线程必须分配资源,那么至少考虑将管理它们的责任移交给子线程。
How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
您是否考虑过使用 std::thread?
我认为以下代码表明您无需使用 std::thread 执行任何操作。 sleep_for() 足够大,可供人类观看。
void pause_thread(int n, std::string lbl)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << lbl << " pause of " << n << " seconds ended" << std::endl;
}
int t403(void)
{
std::cout << "Spawning 3 threads...\n" << std::flush;
std::thread t1 (pause_thread, 6, "t1");
std::thread t2 (pause_thread, 3, "t2");
std::thread t3 (pause_thread, 1, "t3");
std::cout << "Done spawning threads, "
"Note that they finish out-of-order. \n"
"Now 'main' thread waits for spawned threads to join:\n" << std::flush;
t1.join(); std::cout << "join t1 " << std::flush;
t2.join(); std::cout << "join t2 " << std::flush;
t3.join(); std::cout << "join t3 " << std::flush;
std::cout << "completed join \n"
"note: \n - join sequence is in-order, but finish sequence is out-of-order\n"
" - inference: the threads waited for main to join! "<< std::endl;
return(0);
}
更新:2016 年 6 月 1 日
我使用 posix 线程和连接复制了上述方案。看起来主线程不需要通知生成的线程已终止。
此外,来自 "man pthread_join":
The pthread_join() function waits for the thread specified by 'thread'
(parameter 1) to terminate. If that thread has already terminated,
then pthread_join() returns immediately. The thread specified by
thread must be joinable.
关于你的问题,我可能漏掉了什么。
也许发出连接的线程在此期间不应该尝试做任何其他事情。
考虑将 "anything else" 移到它自己的协作线程中?
我要朝不同的方向出发。问题不应该是 "How do I signal end of thread so I can release buf
?" 应该是 "How should I assign ownership for buf
, preferably in such a way that the end of the thread does not matter?"
foo
或 main
应该释放 buf
。那么哪个?
情况 1:buf
已分配但未被 main
使用
这种情况包括 buf
在 main
中分配和初始化,只要 main
在线程完成处理后不消耗 buf
。这个案子很简单。线程取得 buf
的所有权,并在线程完成后将其删除。
void *foo(void *buf)
{
while (running)
{
// Do something with buf
}
delete buf;
return NULL;
}
情况 2:buf
由 main
分配,由 foo
操作,然后由 main
使用。
显然 buf
必须活着才能让 main
消费它,所以 main
应该保留所有权并在消费后删除 buf
。但当?这意味着它必须等待 foo
完成处理 buf
,这意味着加入或完成消息。现在我们得到 John Bollinger 的回答。
情况 3:buf
在 foo
中分配,由 foo
操作,并由 foo
删除。
这样做的好处是永远不会混淆,而且如果 main
从不触及
buf
、main
应该完全放手,包括分配。在一个完美的世界中,它看起来像:
void *foo(void * )
{
char buf[1024];
while (running)
{
// Do something with buf
}
return NULL;
}
缓冲区的大小,如果 main
,但 foo
不知道,可以提供给线程。例如,
pthread_create(&tid, NULL, foo, &bufsize);
和
void *foo(void * size)
{
char * buf = new char[*((int*)size)];
while (running)
{
// Do something with buf
}
delete buf;
return NULL;
}
但你应该更喜欢
void *foo(void * size)
{
std::unique_ptr<char[]> buf(new char[*((int*)size)]);
while (running)
{
// Do something with buf
}
return NULL;
}
避免原始指针。
只要确保 bufsize
在 foo
开始之前没有改变。
我知道通过调用 pthread_join
或 pthread_detach
将在线程完成后释放线程使用的资源,但我的情况并不那么容易。
首先我希望能够从父线程中终止子线程,所以我写了这样的东西:(动态分配的buf在那里显示我似乎找不到使用方法pthread_detach
因为我不知道如何等待子线程完成(所以我可以释放 buf)如果我分离它)
bool running = true;
void *foo(void *buf)
{
while (running)
{
// Do something with buf
}
return NULL;
}
int main(int argc, char **argv)
{
char *buf = new char[1024];
pthread_t tid;
pthread_create(&tid, NULL, foo, buf);
string cmd;
while (true)
{
cin >> cmd;
if (!cmd.compare("stop"))
{
running = false;
pthread_join(tid, NULL);
delete[] buf;
}
else
{
// Do something
}
}
return 0;
}
这似乎有效。但是子线程有时会在父线程想要终止它之前完成。在这种情况下,父线程被阻塞所以
如何通知父线程子线程已经终止,以便父线程可以调用
pthread_join
?有没有一种方法可以使用
pthread_detach
并且仍然能够等待子线程完成以便之后我可以释放 buf(虽然看起来我可以释放子线程中的 buf线程在这个演示中,这对我的实际应用来说是不可能的)?
- How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
有各种各样的选择。以下是三个相当有希望的:
您可以让子线程设置一个全局标志变量。
您可以让子线程用
pthread_kill()
向父线程发送信号。您可以设置一个管道,让子进程在完成后写入,并让父进程从中执行非阻塞读取以测试子进程是否完成。
- Is there a way to use pthread_detach and still be able to wait for the child thread to finish so I can free buf afterwards (though it seems I can free buf in the child thread in this demo, it's impossible for my real application)?
没有。一旦一个线程被分离,它永远不能被加入。
您可以考虑让线程管理自己的资源。或者,如果主线程必须分配资源,那么至少考虑将管理它们的责任移交给子线程。
How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
您是否考虑过使用 std::thread?
我认为以下代码表明您无需使用 std::thread 执行任何操作。 sleep_for() 足够大,可供人类观看。
void pause_thread(int n, std::string lbl)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << lbl << " pause of " << n << " seconds ended" << std::endl;
}
int t403(void)
{
std::cout << "Spawning 3 threads...\n" << std::flush;
std::thread t1 (pause_thread, 6, "t1");
std::thread t2 (pause_thread, 3, "t2");
std::thread t3 (pause_thread, 1, "t3");
std::cout << "Done spawning threads, "
"Note that they finish out-of-order. \n"
"Now 'main' thread waits for spawned threads to join:\n" << std::flush;
t1.join(); std::cout << "join t1 " << std::flush;
t2.join(); std::cout << "join t2 " << std::flush;
t3.join(); std::cout << "join t3 " << std::flush;
std::cout << "completed join \n"
"note: \n - join sequence is in-order, but finish sequence is out-of-order\n"
" - inference: the threads waited for main to join! "<< std::endl;
return(0);
}
更新:2016 年 6 月 1 日
我使用 posix 线程和连接复制了上述方案。看起来主线程不需要通知生成的线程已终止。
此外,来自 "man pthread_join":
The pthread_join() function waits for the thread specified by 'thread' (parameter 1) to terminate. If that thread has already terminated, then pthread_join() returns immediately. The thread specified by thread must be joinable.
关于你的问题,我可能漏掉了什么。
也许发出连接的线程在此期间不应该尝试做任何其他事情。
考虑将 "anything else" 移到它自己的协作线程中?
我要朝不同的方向出发。问题不应该是 "How do I signal end of thread so I can release buf
?" 应该是 "How should I assign ownership for buf
, preferably in such a way that the end of the thread does not matter?"
foo
或 main
应该释放 buf
。那么哪个?
情况 1:buf
已分配但未被 main
这种情况包括 buf
在 main
中分配和初始化,只要 main
在线程完成处理后不消耗 buf
。这个案子很简单。线程取得 buf
的所有权,并在线程完成后将其删除。
void *foo(void *buf)
{
while (running)
{
// Do something with buf
}
delete buf;
return NULL;
}
情况 2:buf
由 main
分配,由 foo
操作,然后由 main
使用。
显然 buf
必须活着才能让 main
消费它,所以 main
应该保留所有权并在消费后删除 buf
。但当?这意味着它必须等待 foo
完成处理 buf
,这意味着加入或完成消息。现在我们得到 John Bollinger 的回答。
情况 3:buf
在 foo
中分配,由 foo
操作,并由 foo
删除。
这样做的好处是永远不会混淆,而且如果 main
从不触及
buf
、main
应该完全放手,包括分配。在一个完美的世界中,它看起来像:
void *foo(void * )
{
char buf[1024];
while (running)
{
// Do something with buf
}
return NULL;
}
缓冲区的大小,如果 main
,但 foo
不知道,可以提供给线程。例如,
pthread_create(&tid, NULL, foo, &bufsize);
和
void *foo(void * size)
{
char * buf = new char[*((int*)size)];
while (running)
{
// Do something with buf
}
delete buf;
return NULL;
}
但你应该更喜欢
void *foo(void * size)
{
std::unique_ptr<char[]> buf(new char[*((int*)size)]);
while (running)
{
// Do something with buf
}
return NULL;
}
避免原始指针。
只要确保 bufsize
在 foo
开始之前没有改变。