线程过早退出

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
}

或更典型地加入等待它们退出的线程:

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

如需了解更多信息,您确实应该查看 this