线程过早退出
Threads exiting prematurely
我有以下一段代码,旨在创建两个线程并无限期地执行它们。但是当运行时,它会在一些迭代后退出。
#include <iostream>
#include "error.h"
#include <cstdlib>
#include <pthread.h>
#include <string>
#include <time.h>
#include <sys/wait.h>
using namespace std;
#define NUM_THREADS 2
#define TIME_OUT 3
void *GoBackN(void* arg) {
while(true) cout<<"Thread executing"<<endl;
}
int main()
{
pthread_t t[NUM_THREADS];
pthread_create((&t[0]),NULL,&GoBackN,NULL);
pthread_create((&t[1]),NULL,&GoBackN,NULL);
wait(NULL);
return 0;
}
输出——线程执行线程执行线程执行线程执行线程执行线程执行线程执行线程执行线程执行线程执行线程执行进程返回0;
我在g++上编译,在linux机器上运行
你有三个线程并且允许主线程退出。
#include <iostream>
#include "error.h"
#include <cstdlib>
#include <pthread.h>
#include <string>
#include <time.h>
#include <sys/wait.h>
using namespace std;
#define NUM_THREADS 2
#define TIME_OUT 3
void* GoBackN(void* arg) {
while(true) cout<<"Thread executing"<<endl;
}
int main() // main thread starts here
{
pthread_t t[NUM_THREADS];
pthread_create((&t[0]),NULL,&GoBackN,NULL); // second thread starts here
pthread_create((&t[1]),NULL,&GoBackN,NULL); // third thread starts here
wait(NULL); // doesn't wait for very long (zero time)
// ...
// main thread keeps running here...
// ...
return 0; // whoops main thread ends closing program
}
你可以在主线程中设置一个无限循环(或无限等待)来阻止它退出程序。
int main()
{
pthread_t t[NUM_THREADS];
pthread_create((&t[0]),NULL,&GoBackN,NULL);
pthread_create((&t[1]),NULL,&GoBackN,NULL);
wait(NULL); // doesn't wait for very long (zero time)
// ...
// loop in the main thread too
while(true) cout<<"Main thread executing"<<endl;
// ...
return 0; // now we don't get here
}
或者更典型的join等待它们退出的线程:
int main() // main thread starts here
{
pthread_t t[NUM_THREADS];
pthread_create((&t[0]),NULL,&GoBackN,NULL); // second thread starts here
pthread_create((&t[1]),NULL,&GoBackN,NULL); // third thread starts here
wait(NULL); // doesn't wait for very long (zero time)
// ...
// join threads here
pthread_join(t[0], nullptr);
pthread_join(t[1], nullptr);
// ...
return 0; // we get here when other threads end
}
现在主线程处于挂起状态,并且在其他线程运行时不会占用任何CPU
时间。
如果你使用的是支持C++11
的现代编译器,你可以像这样使用标准库线程:
#include <thread>
#include <chrono>
#include <vector>
#include <sstream>
#include <iostream>
const int number_of_threads = 5;
// nasty little MACRO to provide synchronized output (crude but works)
#define SYNC_OUT(m) do{std::ostringstream o; o << m << 'n'; std::cout << o.str();}while(0)
void GoBackN(int id) {
while(true)
{
SYNC_OUT("Thread: " << id << " executing");
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
}
int main() // main thread starts here
{
std::vector<std::thread> threads;
for(int i = 0; i < number_of_threads; ++i)
threads.emplace_back(GoBackN, i); // start new thread
// ...
// join threads here
for(auto&& thread: threads)
thread.join();
}
我建议使用<thread>
或<future>
或std::async
。在创建线程之后,您应该稍后.join()
或.detach()
它们,而.join()
会停止主程序的执行,而.detach()
不会。
#include <thread>
#include <iostream>
void foo()
{
std::cout << "print from thread" << std::endl;
}
int main()
{
std::cout << "before the thread starts" << std::endl;
std::thread t(foo);
t.join();
std::cout << "after thread finishes" << std::endl;
}
更多信息,你应该看看这个
相关文章:
- std::async 如果线程是从 DLL 创建的,则会阻止进程退出?
- 在线程退出之前,线程分配的内存块是否与线程本身具有相同的相关性?
- STD ::线程不会退出
- C++线程退出循环条件
- gdb 中的线程 0x7fffc57fa700 (LWP 31671) 退出] 是什么意思?
- 如何使用工作线程正确退出Qt应用程序
- Boost.Asio:由于线程退出或应用程序请求,I/O 操作已中止
- 当子线程退出时,如何通知父线程
- 线程已退出,代码为 1:Join() 和 Detach()
- 安全地退出程序 Win32 C++从分离的 std::线程
- 在另一个线程正在运行的情况下退出应用程序时出错
- C++提升线程组.interrupt_all()导致主线程也退出
- 确定线程是否已退出
- 在退出应用程序时安全退出Qt线程
- c++允许后台线程在退出应用程序之前完成
- 如果线程在调用pthread_join之前退出,该怎么办
- JNI - Java 在本机线程完成执行之前退出
- 线程退出时通知Waiters
- 多线程Qt应用程序在退出时不会停止
- 如何以线程安全的方式退出 C++03 中的程序