Solved

using boost thread for a simple code

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GUI: DIalog Stacking and Popping in MS C++ 4 76
Unable to start eclipse ? 17 141
Safe conversion? 4 69
Why isn't object file created? 6 59
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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