|
Boost Users : |
From: Terry G (tjgolubi_at_[hidden])
Date: 2006-11-24 10:11:38
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 <boost/thread/thread.hpp>
#include <iostream>
#include <windows.h>
using namespace std;
class Child {
const char* c;
public:
typedef void result_type;
Child(const char* x):c(x){ }
void operator()(){
cout << c << " sub thread called." << endl;
}
}; // Child
class Parent {
const char* p;
public:
typedef void result_type;
Parent(const char* x):p(x){}
void operator()(){
cout << p << "thread called." << endl;
Child c1("I am child 1");
boost::thread c1t(ref(c1));
Sleep(10);
}
}; // Parent
int main(){
Parent p1("I am parent 1: ");
boost::thread p1t(ref(p1));
Sleep(10);
return 0;
} // main
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net