C++0x thread interruption

后端 未结 8 1309
谎友^
谎友^ 2020-11-29 04:28

According to the C++0x final draft, there\'s no way to request a thread to terminate. That said, if required we need to implement a do-it-yourself solution.

On the o

相关标签:
8条回答
  • 2020-11-29 05:08

    Using native handle to cancel a thread is a bad option in C++ as you need to destroy all the stack allocated objects. This was the main reason they don't included a cancel operation.

    Boost.Thread provides an interrupt mechanism, that needs to pool on any waiting primitive. As this can be expensive as a general mechanism, the standard has not included it.

    You will need to implement it by yourself. See my answer here to a similar question on how to implement this by yourself. To complete the solution an interruption should be throw when interrupted is true and the thread should catch this interruption and finish.

    0 讨论(0)
  • 2020-11-29 05:08

    I agree with this decision. For example, .NET allows to abort any worker thread, and I never use this feature and don't recommend to do this to any professional programmer. I want to decide myself, when a worker thread may be interrupted, and what is the way to do this. It is different for hardware, I/O, UI and other threads. If thread may be stopped at any place, this may cause undefined program behavior with resource management, transactions etc.

    0 讨论(0)
提交回复
热议问题