Hi
the code below compiles and runs, but does not do what I want.
how can I get the code below to print out:
"I am child 1sub thread called."
thanks
******************************************************************
#include
On 24/11/06, Fred J.
Hi the code below compiles and runs, but does not do what I want. how can I get the code below to print out: "I am child 1sub thread called."
thanks
**************************************** ************************** #include
#include <string> #include <iostream> using namespace std; class Child { string c; public: Child(string x):c(x){ } void operator()(){ cout << c << " sub thread called." << endl; } };
class Parent { string p; public: Parent(string x):p(x){} void operator()(){ cout << p << "thread called." << endl; Child c1("I am child 1"); boost::thread c1t(c1); } };
int main(){ Parent p1("I am parent 1: "); boost::thread p1t(p1); }
Hello, I think you need to join both threads c1t and p1t (i.e. c1t.join() and p1t.join()) before either object goes out of scope lest the thread objects themselves are destroyed (and main() exits) prior to the threads themselves completing their work. Regards, Jos Hickson
Fred J." wrote
The code below compiles and runs, but does not do what I want.
how can I get the code below to print out:
"I am child 1sub thread called."
Hi Fred!
Three things I noticed.
I'm not a user of boost.thread, but I did get your example to work.
Three things.
(1) I think you want to pass a reference to p1 and c1, rather than copy
them.
(2) std:string is not thread-safe.
(3) You need to delay to allow the other threads time to run.
#include
I wrote:
Three things I noticed. I'm not a user of boost.thread, but I did get your example to work. Three things.
(1) I think you want to pass a reference to p1 and c1, rather than copy them. (2) std:string is not thread-safe. (3) You need to delay to allow the other threads time to run.
Sorry, I hacked when I should have been thinking. (3) thread.join() is much better than delay. terry
participants (4)
-
Fred J.
-
Jos Hickson
-
Marco De Paoli
-
Terry G