线程过早退出

Threads exiting prematurely

本文关键字:退出 线程      更新时间:2023-10-16

我有以下一段代码,旨在创建两个线程并无限期地执行它们。但是当运行时,它会在一些迭代后退出。

#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;
}

更多信息,你应该看看这个