【问题标题】:How to find out if other threads are running?如何判断其他线程是否正在运行?
【发布时间】:2017-05-11 08:24:03
【问题描述】:

我有一个“监视线程”,它检查其他线程是否正在运行并计算一些数据。如果这些线程结束,我也想完成我的观察线程。我该怎么做?

#include <iostream>
#include <thread>

using namespace std;

void f1() {
    cout << "thread t1" << endl;
    for (int i=0; i<1000; ++i) {
       cout << "t1: " << i << endl;
    }   
}

void f2() {
    cout << "thread t2" << endl;
    while (T1_IS_RUNNING) {
       cout << "t1 still running" << endl;
    }   
}

int main() {
    thread t1(f1);
    thread t2(f2);

    t1.join();
    t2.join();

    return 0;
}

在上面的例子中我需要实现T1_IS_RUNNING。任何想法如何做到这一点?我的猜测是获取正在运行的线程数,但我在 STL 中没有找到任何相关方法。

已经有一个How to check if a std::thread is still running?,但我认为他们对我的情况使用了过于复杂的解决方案。一个简单的线程计数器 (std::atomic) 还不够好吗?

【问题讨论】:

标签: c++ multithreading c++11


【解决方案1】:

您可以只使用一个标志 (running example):

#include <iostream>
#include <thread>

using namespace std;

bool T1_IS_RUNNING = true;
void f1() {
    cout << "thread t1" << endl;
    for (int i=0; i<1000; ++i) {
       cout << "t1: " << i << endl;
    }
    T1_IS_RUNNING = false;
    cout << "thread t1 finish" << endl;
}

void f2() {
    cout << "thread t2" << endl;
    while (T1_IS_RUNNING) {
       cout << "t1 still running" << endl;
    }   
    cout << "thread t2 finish" << endl;
}

int main() {
    thread t1(f1);
    thread t2(f2);

    t1.join();
    t2.join();

    return 0;
}

这是安全的,只要其中一个写入标志而另一个读取它,否则您需要使用原子标志、互斥体或信号量。

【讨论】:

  • 在这种情况下它可以工作,我会使用计数器来处理超过 2 个线程。
  • 使用std::atomic&lt;bool&gt;,否则你有UB。
  • 这可能确实编译为if (T1_IS_RUNNING) while(true) { ... - 这是UB 中最友好的。
  • 这是一场数据竞赛,你有 UB。 bool 在几乎所有可能的架构上始终是原子的,这仍然不是这不是 UB 的原因。 Standard on data race
  • @PasserBy,如果只有一个线程写入,则没有数据,请记住这一点。
【解决方案2】:

使用 atomic_int:

int main(){
    std::atomic_int poor_man_semaphore{0};
    poor_man_semaphore++;
    std::thread t1([&]()
    {
        std::this_thread::sleep_for(std::chrono::seconds(100));
        poor_man_semaphore--;
    });
    poor_man_semaphore++;
    std::thread t2([&]()
    {
         std::this_thread::sleep_for(std::chrono::seconds(1));
         poor_man_semaphore--;
    });
    poor_man_semaphore++;
    std::thread t3([&]()
    {
         std::this_thread::sleep_for(std::chrono::seconds(1));
         poor_man_semaphore--;
    });

    t2.join();
    t3.join();
    while ( poor_man_semaphore > 0 )
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));

    }
    t1.join();
    return 0;
}

【讨论】:

    【解决方案3】:

    让我快速修复一下代码,因为已经有一个detailed post,这不会很长。

    这个答案存在是因为这里有很多错误的答案。

    我对您的问题的解释是,您希望“监视线程”在其他线程仍然存在的情况下工作,但在其他线程停止时停止。

    #include <fstream>
    #include <thread>
    #include <atomic>  // this is REQUIRED, NOT OPTIONAL
    
    using namespace std;
    
    atomic_int count(1);  // REQUIRED to be atomic
    
    void f1() {
        ofstream f1out{"f1out.txt"};
        f1out << "thread t1" << endl;
        for (int i=0; i<1000; ++i) {
           f1out << "t1: " << i << endl;
        }
        count--;
    }
    
    void f2() {
        ofstream f2out{"f2out.txt"};
        f2out << "thread t2" << endl;
        while (count > 0) {
           f2out << "t1 still running" << endl;
        }
    }
    
    int main() {
        thread t1(f1);
        thread t2(f2);
    
        t1.join();
        t2.join();
    }
    

    关于atomic的注释

    atomic_int 的语法可能看起来像 int,但它们是不同的,未能使用 atomic_int 是未定义的行为。

    来自[intro.races],强调我的

    如果其中一个修改了内存位置,而另一个读取或修改了相同的内存位置,则两个表达式计算会发生冲突。 [...]

    如果程序的执行包含两个潜在的并发冲突操作,则程序的执行包含数据竞争,其中至少一个不是原子的,并且都不会在另一个之前发生 [...] 。 任何此类数据竞争都会导致未定义的行为。

    关于cout的注释

    同样,如果线程同时使用cout,这是一场数据竞争,我找不到简单的替换来保留含义和效果。最后我选择使用ofstream。

    致相关人士

    是的,原子操作不需要顺序一致,但这确实无助于清晰。

    【讨论】:

      【解决方案4】:

      这个link 可能会对您有所帮助。 在众多解决方案中,一个似乎很容易实现:

      一个简单的解决方案是有一个布尔变量,线程定期将其设置为 true,然后由想要知道状态的线程检查并设置为 false。如果变量为假太长,则线程不再被视为活动。
      更线程安全的方法是让子线程增加一个计数器,然后主线程将计数器与存储的值进行比较,如果在太长时间后相同,则认为子线程不活动。

      也许你可以设置一个布尔数组,一个一个你运行的线程,然后在你想知道其他线程是否正在运行时检查它?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        相关资源
        最近更新 更多