Solved

using boost thread for a simple code

Posted on 2006-11-23
8
858 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now