根据C++ 0x最终草案,没有办法请求线程终止.也就是说,如果需要,我们需要实施一个自己动手的解决方案.
另一方面,boost :: thread提供了一种以安全方式中断线程的机制.
在您看来,什么是最好的解决方案?设计自己的合作"中断机制"还是本土的?
我有兴趣在c ++中终止/停止/杀死分离的线程.如何才能做到这一点?
void myThread()
{
int loop = 0;
while(true)
{
std::this_thread::sleep_for(std::chrono::seconds(5));
++loop;
}
}
void testThread()
{
std::thread globalThread(myThread);
globalThread.detach();
}
int main(void)
{
testThread();
for(unsigned int i=0; i < 1000; i++)
{
cout << "i = " << i << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我之所以要"停止"/"终止"globalThread(),是因为valgrind列出了这种"可能丢失"的内存泄漏类型(152字节).处理这个问题的最佳方法是什么?
我想知道调用pthread_cancel()终止线程是否安全.我在手册页中找不到任何提示.提前感谢任何提示.
编辑:也许我不够准确.我不是在讨论由早期的pthread_cancel()终止的线程,而是关于只是从它们的线程函数返回的线程.
我正在使用a SynchronisedQueue来进行线程之间的通信.我发现当附加线程在条件变量上等待时销毁线程对象会导致程序崩溃.这可以通过detach()在线程销毁之前调用来纠正.但我想知道等待条件变量的线程终止后会发生什么.有没有其他方法可以使用条件变量来避免这种情况?
#include <queue>
#include <thread>
#include <mutex>
#include <condition_variable>
template <typename Type> class SynchronisedQueue {
public:
void Enqueue(Type const & data) {
std::unique_lock<std::mutex> lock(mutex_);
queue_.push(data);
condition_.notify_one();
}
Type Dequeue() {
std::unique_lock<std::mutex> lock(mutex_);
while (queue_.empty())
condition_.wait(lock);
Type result = queue_.front();
queue_.pop();
return result;
}
private:
std::queue<Type> queue_;
std::mutex mutex_;
std::condition_variable condition_;
};
class Worker {
public:
Worker(SynchronisedQueue<int> * queue) : queue_(queue) {}
void operator()() {
queue_->Dequeue(); // <-- The thread waits here.
}
private:
SynchronisedQueue<int> * queue_; …Run Code Online (Sandbox Code Playgroud) 我想用
scalaz.concurrent.Task实现我的异步处理.我需要一个函数(Task[A], Task[B]) => Task[(A, B)]来返回一个工作如下的新任务:
Task[A]并Task[B]等待结果;你会如何实现这样的功能?
假设我有一个原型如下所示的函数,属于类container_class:
std::vector<int> container_class::func(int param);
Run Code Online (Sandbox Code Playgroud)
该函数可能会或可能不会在某些输入上产生无限循环; 无法确定哪些输入会导致成功,哪些输入会导致无限循环.该函数在一个库中,我没有源代码,也无法修改(这是一个bug,将在几个月内在下一个版本中修复,但是现在我需要一种方法来解决它),所以修改函数或类的解决方案将不起作用.
我尝试使用std::async和隔离函数std::future,并使用while循环来不断检查线程的状态:
container_class c();
long start = get_current_time(); //get the current time in ms
auto future = std::async(&container_class::func, &c, 2);
while(future.wait_for(0ms) != std::future_status::ready) {
if(get_current_time() - start > 1000) {
//forcibly terminate future
}
sleep(2);
}
Run Code Online (Sandbox Code Playgroud)
这段代码有很多问题.一个是我无法强制终止std::future对象(以及它所代表的线程).
在极端情况下,如果我找不到任何其他解决方案,我可以在自己的可执行文件中隔离该函数,运行它,然后检查其状态并适当地终止它.但是,我宁愿不这样做.
我怎么能做到这一点?有没有比我现在做的更好的方式?
我有以下代码:
class Foo {
private:
std::thread thread;
void run();
std::atomic_flag running;
std::thread::native_handle_type native;
public:
Foo(const std::string& filename);
virtual ~Foo();
virtual void doOnChange();
void start();
void quit();
};
#include "Foo.h"
#include <functional>
#include <iostream>
Foo::Foo(const std::string& filename) :
thread(), running(ATOMIC_FLAG_INIT) {
file = filename;
native = 0;
}
Foo::~Foo() {
quit();
}
void Foo::start() {
running.test_and_set();
try {
thread = std::thread(&Foo::run, this);
} catch (...) {
running.clear();
throw;
}
native = thread.native_handle();
}
void Foo::quit() {
running.clear();
pthread_cancel(native);
pthread_join(native, nullptr);
//c++11-style …Run Code Online (Sandbox Code Playgroud) 我有一个程序创建一个监听事件的线程。有一种情况,该线程永远不会收到该事件,我必须终止它。我知道如何捕获它不会收到此事件的情况。
我创建了一个std::thread,但是我没有找到有关如何终止线程的信息。我试过打电话
t.detach()
Run Code Online (Sandbox Code Playgroud)
然后让析构函数继续工作,但我无法以这种方式清理线程分配的资源。
我的问题是: 1. 有没有办法发送 SIGKILL 或等效的 a 来std::thread杀死它?2.有没有办法从线程内捕获这个信号来清理资源?
提出(并回答)的问题如何在 C++11 中终止线程?没有回答我的问题,因为我想从其父级终止线程,而不是从线程内终止线程。
此外,我的问题是我的工作线程可能正在等待阻塞系统调用(即传入数据包)或条件变量,这就是为什么这很棘手。
我在 C++11 代码中运行多个线程,并且线程体是使用 lambda 函数定义的,如下所示。
// make connection to each device in a separate child thread
std::vector<std::thread> workers;
for(int ii = 0; ii < numDev; ii++)
{
workers.push_back(std::thread([=]() { // pass by value
// thread body
}));
}
// detach from all threads
std::for_each(workers.begin(), workers.end(), [](std::thread &t) {
t.detach();
});
// killing one of the threads here?
Run Code Online (Sandbox Code Playgroud)
我与所有子线程分离,但在工人向量中保留每个子线程的引用。如何稍后在代码中杀死其中一个线程?
这里的帖子建议使用std::terminate(),但我想它对我来说没有用。
我最近在C++ 11中使用线程.现在我正在考虑如何强制停止一个线程.我无法在stackoverflow上找到它,也试过这些.
我没有更多的想法.我听说过WinAPI,但我想要一个便携式解决方案.(这也意味着我不会使用fork())
你能帮我解决一个问题吗?我真的很想这样做.
我正在编写 C++ 代码来在不同线程上执行任务并等待结果返回主函数。
每个线程可以采取不同的计时,并可能导致正确或不正确的结果。
我按如下方式启动线程:
std::thread p1(&process1, (void*)&p1Data);
std::thread p2(&process2, (void*)&p2Data);
std::thread p3(&process3, (void*)&p3Data);
.
.
.
Run Code Online (Sandbox Code Playgroud)
(多个线程就是这样创建的)。
流程功能可以总结如下:
\\ThreadData is a structure that contains all the data for the thread
void process1(void *data)
{
ThreadData * tData = (ThreadData*) data;
ResultObject * result = data->result;
.
.
.
\\Process to calculate Result using data
}
Run Code Online (Sandbox Code Playgroud)
在启动这些线程后,我将在 main 中加入它们以等待最终结果,然后检查正确的结果。
p1.join();
p2.join();
.
.
.
p1result = p1Data.result;
p2result = p2Data.result;
.
.
.
.
.
.
if (p1result)
{
return …Run Code Online (Sandbox Code Playgroud) 有没有办法从另一个取消boost :: thread,如下所示?:
boost::thread* thread1(0);
boost::thread* thread2(0);
thread2 = new boost::thread([&](){
//some expensive computation that can't be modified
if(thread1)
thread1->interrupt();
});
thread1 = new boost::thread([&]() {
//some other expensive computation that can't be modified
if(thread2)
thread2->interrupt();
});
thread1->join();
thread2->join();
delete thread1;
delete thread2;
Run Code Online (Sandbox Code Playgroud)
现在,两个昂贵的计算都完成而不会被打断.我认为连接将被视为中断点,主线程将在完成两个昂贵的计算之一后继续.