使用升压线:信号并等待终止 [英] Using a boost thread: Signal and wait for termination
问题描述
我目前正在写的后来大多在Delphi中使用C / C ++ DLL和我比较熟悉德尔福线程比C / C ++,特别是提振。所以,我不知道如何才能达到以下情形?
i'm currently writing a c/c++ dll for later use mostly in Delphi and i'm more familiar with threads in Delphi than c/c++ and especially boost. So i wonder how i can achieve the following scenario?
class CMyClass
{
private:
boost::thread* doStuffThread;
protected:
void doStuffExecute(void)
{
while(!isTerminationSignal()) // loop until termination signal
{
// do stuff
}
setTerminated(); // thread is finished
};
public:
CMyClass(void)
{
// create thread
this->doStuffThread = new boost::thread(boost::bind(&CMyClass::doStuffExecute, this));
};
~CMyClass(void)
{
// finish the thread
signalThreadTermination();
waitForThreadFinish();
delete this->doStuffThread;
// do other cleanup
};
}
我对升压线程,信号和互斥红色无数的文章,但我不明白这一点,也许是因为今天是星期五;?)或者是不可行的如何,我认为做到这一点。
I have red countless articles about boost threading, signals and mutexes but i don't get it, maybe because it's friday ;) or is it not doable how i think to do it?
问候
丹尼尔
推荐答案
只需使用一个原子布尔告诉线程停止:
Just use an atomic boolean to tell the thread to stop:
class CMyClass
{
private:
boost::thread doStuffThread;
boost::atomic<bool> stop;
protected:
void doStuffExecute()
{
while(!stop) // loop until termination signal
{
// do stuff
}
// thread is finished
};
public:
CMyClass() : stop(false)
{
// create thread
doStuffThread = boost::thread(&CMyClass::doStuffExecute, this);
};
~CMyClass()
{
// finish the thread
stop = true;
doStuffThread.join();
// do other cleanup
};
}
要等待线程来完成你刚刚加入,这将阻止,直到它完成,并且可以加入。您需要反正加入线程之前,你可以摧毁它,或者它会终止你的程序。
To wait for the thread to finish you just join it, that will block until it is finished and can be joined. You need to join the thread anyway before you can destroy it, or it will terminate your program.
有没有必要使用指针,并创建新
,只需要使用的boost ::线程
直接对象。在堆上创建一切浪费,不安全和贫穷的风格。
There is no need to use a pointer and create the thread with new
, just use a boost::thread
object directly. Creating everything on the heap is wasteful, unsafe and poor style.
有没有必要使用的boost ::绑定
来传递参数给线程
构造函数。很多年的boost ::线程
已经支持直接传递多个参数给它的构造和它内部的绑定。
There is no need to use boost::bind
to pass arguments to the thread
constructor. For many many years boost::thread
has supported passing multiple arguments to its constructor directly and it does the binding internally.
这是停止
已被初始化为这一点很重要虚假
创建新线程之前,否则,如果新的线程正在催生很快就可以检查的值停止
之前被初始化,并且可能发生读真正
价值来自未初始化的内存,然后它会不会进入循环。
It's important that stop
has been initialized to false
before the new thread is created, otherwise if the new thread is spawned very quickly it could check the value of stop
before it is initialized, and might happen to read a true
value from the uninitialized memory, and then it would never enter the loop.
在风格的主题,写富(无效)
被许多C ++程序员认为是一种令人厌恶的可憎。如果你要说出你的函数没有参数,然后只写富()
。
On the subject of style, writing foo(void)
is considered by many C++ programmers to be a disgusting abomination. If you want to say your function takes no arguments then just write foo()
.
这篇关于使用升压线:信号并等待终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!