Problem implementing threads in a program
<p>I have the following program:</p>

<pre><code>#include "stdafx.h"
#include &lt;boost/thread/thread_time.hpp&gt;
#include &lt;boost/thread/thread.hpp&gt;
#include &lt;iostream&gt;
using namespace std;

class DogDoor {
    bool open;
public:
    DogDoor()
    {
        this-&gt;open = false;
    }
    void Open()
    {
        cout &lt;&lt; "The dog door opens" &lt;&lt; endl &lt;&lt; endl;
        open = true;
    }
    void close()
    {
        cout &lt;&lt; "The dog door closes" &lt;&lt; endl &lt;&lt; endl;
        open = false;
    }
    bool isOpen()
    {
        return open;
    }
};

class Remote {
    DogDoor* door;
public:
    Remote(DogDoor* door)
    {
        this-&gt;door = door;
    }
    void pressButton()
    {
        cout &lt;&lt; "Pressing the remote button" &lt;&lt; endl;
        if(door-&gt;isOpen())
        {
            door-&gt;close();
        }
        else
        {
            door-&gt;Open();
            // close after 5 seconds
            boost::posix_time::seconds workTime(5);
            boost::this_thread::sleep(workTime);
            door-&gt;close();
        }
    }
};

void threadFunc(Remote* r)
{
    r-&gt;pressButton();
}

int _tmain(int argc, _TCHAR* argv[])
{
    DogDoor* door = new DogDoor();
    Remote* remote = new Remote(door);

    cout &lt;&lt; "Fido barks to go outside" &lt;&lt; endl;
    boost::thread workerThread(threadFunc, remote);

    cout &lt;&lt; "Fido has gone outside" &lt;&lt; endl;

    cout &lt;&lt; "Fido is all done..." &lt;&lt; endl;

    cout &lt;&lt; "Fido is back inside" &lt;&lt; endl;

    workerThread.join();

    delete door;
    delete remote;

    return 0;
}
</code></pre>

<p>I want the program to print:</p>

<pre><code>Fido barks to go outside
Pressing the remote button
The dog door opens
Fido has gone outside
Fido is all done...
Fido is back inside
// and then after 5 seconds it should print this
The dog door closes 
</code></pre>

<p>But my implementation it will print like this and I don't know how to make it print good:</p>

<pre><code>Fido barks to go outside
Fido has gone outside
Fido is all done...
Fido is back inside
Pressing the remote button
The dog door opens
// this after 5 seconds
The dog door closes
</code></pre>
