Solved

using boost thread for a simple code

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 72
installing and using WTP plugin eclipse MARS 3 97
Eclipse. How to move up/down source folders 4 74
Safe conversion? 4 57
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

911 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

17 Experts available now in Live!

Get 1:1 Help Now