std :: thread - “在没有活动异常的情况下调用”,不想“加入”它 [英] std::thread - "terminate called without an active exception", don't want to 'join' it

查看:340
本文介绍了std :: thread - “在没有活动异常的情况下调用”,不想“加入”它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据此问题,我正在使用线程终止关于用户输入的功能。我的代码看起来像:

As per This Question, I'm using a thread to terminate a function on user input. My code looks something like:

bool stopper = false;
thread stopThread(userStop, &stopper);      // start thread looking for user input
for(int i = 0; i < 1000; i++) {
    if(stopper) { break; }                  // break if desired
    // Do stuff
}
return 0;

其中,

userStop(bool *st) {
    char chChar = getchar();
    if(chChar == '\n') {
        *st = true;
    }
}



当我运行这个,我得到错误 terminate在没有活动异常的情况下调用。基于以下问题:线程终止调用时没有活动异常, a href =http://stackoverflow.com/questions/7381757/c-terminate-called-without-an-active-exception-producer-consumer>在没有活动异常生成器/消费者的情况下调用c ++ terminate;它看起来像它,因为我不再加入线程。

When I run this, I get the error terminate called without an active exception. Based on these questions: thread terminate called without an active exception, c++ terminate called without an active exception producer/consumer; it looks like its because I'm not 'join'ing the thread again.

问题是,我不想'加入'线程 - 因为那么用户将需要 userStop()提供输入以终止,但我只希望用户提供

The problem is, I don't want to 'join' the thread -- because then the user will need to provide input for userStop()to terminate, but I only want the user to provide input if the for-loop is to be broken (which it isn't necessarily).

谢谢!

推荐答案

你遇到的麻烦是 stopThread 超出堆栈范围的结果。 C ++标准有如下说明:

The trouble you are encountering is a result of the stopThread going out of scope on the stack. The C++ standard has the following to say about this:


30.3.1.3 thread destructor [thread.thread。 destr]

30.3.1.3 thread destructor [thread.thread.destr]

〜thread();

~thread();


) then terminate(),否则没有效果。 [注意:
在其析构函数
中隐式分离或加入 joinable()线程可能会导致难以调试正确性
性能(加入)错误只有当异常是
时才会遇到。因此,程序员必须确保在线程仍然可以连接的时候析构函数从不
被执行。 - 结束注释]

If joinable() then terminate(), otherwise no effects. [ Note: Either implicitly detaching or joining a joinable() thread in its destructor could result in difficult to debug correctness (for detach) or performance (for join) bugs encountered only when an exception is raised. Thus the programmer must ensure that the destructor is never executed while the thread is still joinable. — end note ]


让线程超出范围,而不先调用 join() detatch()

What this means is that you should not let threads go out of scope without first calling either join() or detatch().

你描述它的方式,你希望线程超出范围,没有加入,所以它将继续运行作为你的应用程序运行。这需要调用 detach()。从那里,我只能提供一点智慧...

The way you describe it, you want the thread to go out of scope without joining so it will continue to run as your application runs. That requires a call to detach(). From there, I can only offer a little wisdom...


  • 这个线程现在完全负责自己的生命。

  • That thread is now completely responsible for its own lifetime. If it doesn't return on its own, it will run forever (until the process terminates).

您正在接收用户输入,可能来自类似 cin getch()。如果这些从多个线程访问,您没有太多保证在它们的库实现中没有竞争条件。

You are getting user input, presumably from something like cin or getch(). If these are accessed from multiple threads, you do not have much assurance that there are not race conditions in their library implementations. Tread lightly.

这篇关于std :: thread - “在没有活动异常的情况下调用”,不想“加入”它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆