Multithreading like in java with boost

I

ironpingwin

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;
}
 
S

Szabolcs Ferenczi

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
 
J

James Kanze

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;

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;

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top