From outside the function operator()(), I can have the pointer to
I have a strange problem with the code below. thread object 'thread'.
From inside operator()() 'thread' is always 0.
Why does this happen?
------------------------------------------------------------------
#include <cstdio>
#include
On Jan 7, 2008, at 7:16 AM, Chameleon wrote:
I have a strange problem with the code below.
From outside the function operator()(), I can have the pointer to thread object 'thread'. From inside operator()() 'thread' is always 0.
Why does this happen?
------------------------------------------------------------------ #include <cstdio> #include
class A { boost::thread *thread;
public: A() : thread(0) {} ~A() { join(); }
void createThread() { thread = new boost::thread(*this); }
void operator()() { printf("Starting thread with pointer: %d\n", thread); //<<<<<<<<<<<<<
void join() { if (thread) thread->join(); } };
int main(int, char**) { A a; a.createThread(); a.join(); return 0; }
When you create "a", thread is 0. Inside of createThread you *first* copy *this, passing the copy to create a boost::thread, and *then* assign the local thread* a value. -Howard
participants (2)
-
Chameleon
-
Howard Hinnant