I got confused with the following code (from Prefer to use active objects instead of a bare topic ):
class Active {
public:
class Message {
public:
virtual ~Message() { }
virtual void Execute() { }
};
private:
unique_ptr<Message> done;
message_queue< unique_ptr<Message> > mq;
unique_ptr<thread> thd;
private:
void Run() {
unique_ptr<Message> msg;
while( (msg = mq.receive()) != done ) {
msg->Execute();
}
}
public:
Active() : done( new Message ) {
thd = unique_ptr<thread>(
new thread( bind(&Active::Run, this) ) );
}
...
(After the constructor completion element variables are only available in the element stream).
Can someone explain to me the reasons why in this case it is safe to exchange an Active object thisbetween threads? Do we have any guarantees that the new thread will see the changes made to the constructor before the thread thread line?
source
share