简体   繁体   English

如何停止和销毁主线程(从中创建线程)

[英]How to stop and destroy thread from main thread (from which is created)

I need to create, run, stop thread and then again same process (reloading some new data and need to refresh and cannot use C++11 standard). 我需要创建,运行,停止线程,然后再次执行相同的过程(重新加载一些新数据,并且需要刷新并且不能使用C ++ 11标准)。 I have created and run thread like from mine main thread 我已经从我的主线程创建并运行了线程

pthread_t p;
pthread_create(&p, NULL, calculation, some_pointer_to_object);

How to stop and destroy this thread from main thread ? 如何从主线程停止并销毁此线程? (pthread_exit is from current thread). (pthread_exit来自当前线程)。

您需要使用pthread_cancel()

The only clean way to do so is this: Set up a flag in the main thread, start the thread, poll the flag in your new thread and finish fast if it's set. 这样做的唯一干净方法是:在主线程中设置一个标志,启动该线程,在新线程中轮询该标志,并在设置完成后快速完成。 Everything else but letting your new thread close itself down cleanly on request opens a boatload of cans of worms, and that's an understatement. 除了让您的新线程根据要求完全关闭自己以外,其他所有操作都会导致一大堆蠕虫,这是一种轻描淡写的说法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM