By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,133 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,133 IT Pros & Developers. It's quick & easy.

Multithreading like in java with boost

P: n/a
Hi!

I'd like to make few threads which will run in the same time in C++.

I try to use boost library v 1.34.1 (it can't be newest, because I
compile on remote machine, which is not administrated by me). In this
version there isn't detach() function.
How to run functions from two different class in the same time?
In my example two go() function from classes first and second should
run in the same time, but they don't.
Thanks for any help.

#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>

class first {
public:
void operator()() { }
void go () {
for (int i=0; i<= 10; ++i) {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 1;

boost::thread::sleep(xt);

std::cout << "first" << std::endl;
}
}
};

class second {
public:
void operator()() { }
void go () {
for (int i=0; i<= 10; ++i) {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 1;

boost::thread::sleep(xt);

std::cout << "second" << std::endl;
}
}
};
int main(int argc, char* argv[])
{
first f;
second s;

boost::thread t1(f);
boost::thread t2(s);
f.go();
s.go();

return 0;
}
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On May 10, 12:55*am, ironpingwin <Skrzy...@gmail.comwrote:
How to run functions from two different class in the same time?
In my example two go() function from classes first and second should
run in the same time, but they don't.
Thanks for any help.
You must define the activity in the operator()

void operator()() { go(); }

Then, instead of activating the actions sequentially
f.go();
s.go();
you must wait for the threads to finish
t1.join();
t2.join();

It will run so much in the same time that the output may be mixed from
the two threads. You can protect the output stream with a lock, of
course.

Best Regards,
Szabolcs
Jun 27 '08 #2

P: n/a
On 10 mai, 00:55, ironpingwin <Skrzy...@gmail.comwrote:
I'd like to make few threads which will run in the same time
in C++.
I try to use boost library v 1.34.1 (it can't be newest, because I
compile on remote machine, which is not administrated by me). In this
version there isn't detach() function.
As far as I know, there isn't in the latest versions either.
Basically, if you destruct the thread object, the thread is
detached automatically. (More of a design flaw than a feature,
but that's the way it is.)
How to run functions from two different class in the same
time? In my example two go() function from classes first and
second should run in the same time, but they don't. Thanks
for any help.
The functions will run in the thread where you call them.
Boost::thread expects a function or a functional object (an
object which defined operator()), and calls it. After copying
the object: you mention Java in the header---Boost uses a
completely different philosophy.
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <iostream>
class first {
public:
void operator()() { }
void go () {
for (int i=0; i<= 10; ++i) {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
std::cout << "first" << std::endl;
}
}
};
class second {
public:
void operator()() { }
void go () {
for (int i=0; i<= 10; ++i) {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);
std::cout << "second" << std::endl;
}
}
};
int main(int argc, char* argv[])
{
first f;
second s;

boost::thread t1(f);
boost::thread t2(s);
f.go();
s.go();
return 0;
}
In the above:

1. put the call to go() in the operator()() of each class,
2. don't call go from the main thread, and
3. call join on each of the threads before returning.

(If you don't do the last step, you'll terminate the process
before either of the threads will have had time to run.)

If you want detached threads, use a separate function to create
them, with the boost::thread object on the stack. (Returning
from the function will detach the thread.) And don't forget to
add some sort of logic to ensure that you don't return from main
until all of the threads have finished.

If you want to join with the thread, and use data written in the
thread object by the thread, after the join, be sure to ensure
that the copy isn't deep (since the thread will actually run on
a copy), and that the data is managed through a pointer of some
sort.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.