?
Solved

using boost thread for a simple code

Posted on 2006-11-23
8
Medium Priority
?
866 Views
Last Modified: 2013-12-14
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 <boost/thread/thread.hpp>
#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);
}
 
0
Comment
Question by:samj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
8 Comments
 
LVL 3

Expert Comment

by:Akumas
ID: 18006817
not quite clear...

first , cout is not thread safe, the output result is hard to predict.
second, after you create thread, us thread.join to wait thread finish.

like this:

    Child c1("I am child 1");
    boost::thread c1t(c1);
    c1t.join();//wait thread finish
0
 

Author Comment

by:samj
ID: 18009061
that did not quite fix it. however the fix was to add join() to each of the Parent and Child objects like this

#include <boost/thread/thread.hpp>
#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);
    c1t.join();
  }
};
 
int main(){
  Parent p1("I am parent 1: ");
  boost::thread p1t(p1);
  p1t.join();
}
 
0
 

Author Comment

by:samj
ID: 18009096
I will ask to split the points and award you 75 for your attempt.
0
 
LVL 3

Expert Comment

by:Akumas
ID: 18010277
yes, it's just a example.
anyway, it's a pleasure to help:)
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 19251587
PAQed with points refunded (0)

Computer101
EE Admin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question