多次使用同一线程的 POSIX pthread
POSIX pthread using same thread multiple times
我目前正在处理的问题,我所相信的,是一个相当简单的问题,我似乎无法解决。
我的程序中有两个线程。线程运行良好,但导致问题的是一个线程的重用。伪代码中的以下内容:
main() {
create thread 1;
create thread 2;
join thread 1;
}
thread 1 {
while true
if(some condition)
join thread 2
// Use the returned value from thread 2
}
thread 2 {
while true
if(some condition)
// do something
exit thread 2(with some return value to thread 1).
}
因此,当线程 1 中满足某些条件时,我希望它终止线程 2,直到它完成,这工作得很好。线程 2 到达 is 条件并退出线程。但是,当我回到线程 1 的 while 循环并再次达到条件时,我希望它再次重新运行线程 2。这就是导致问题的原因。执行线程 2 一次后,线程 1 忽略我的 join 语句,只在 while 循环中轮询,是唯一正在运行的线程。
所以我的问题是。如何重用联接线程 2 属性,使程序持续运行?
执行线程 2 一次后,线程 1 忽略我的 join 语句,只在 while 循环中轮询, 是唯一正在运行的线程。
请注意,在您的情况下,pthread_join(( 很可能会失败,并显示错误 2. 时间。检查其返回值。
但是,由于线程 2 已退出,因此没有线程可等待。您必须重新启动线程 2。那是:
thread 1 {
while true
if(some condition)
join thread 2
// Use the returned value from thread 2
create thread 2;
}
相关文章:
- 如何在C++中挂起和恢复 POSIX 线程?
- 线程SCHED_RR Posix 计时器使用的是 100% CPU
- Posix 线程类和启动例程 (pthread)
- POSIX 的问题。线程无法正常工作
- Qt 中的 Posix 线程
- 如何在一个POSIX线程中组合两种不同的等待机制
- POSIX线程,将多个参数传递给具有结构的函数
- 是否允许主线程在进入main()之前生成POSIX线程
- C 11 STD ::线程与POSIX线程
- POSIX:我如何暂停线程
- POSIX线程 - (MUTEX和条件变量)问题
- C++IO/多路复用TCP服务器和POSIX线程
- 如何在C++中安全地销毁 Posix 线程池
- 在POSIX中,我可以保存信号以供调用条件等待的其他线程使用吗.(这些线程来自同一进程)
- std::condition_variable::wait()是POSIX线程取消点吗
- POSIX 线程 - 使用条件变量 MEMORY LEAK 同步分离的线程
- 使用Posix时间的Boost线程超时
- 尝试创建 posix 线程并获得从 'void*' 到 'void* (__attribute__((__cdecl__)) *)(void*) 错误的无效转换
- C++ - 在后台 POSIX 线程上使用固定增量时间循环
- 等待多个线程(Posix线程,c++)