472,954 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

Multithreading like in java with boost

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
2 2357
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brzezi | last post by:
Hi. I`m mostly java programmer, but I know a C++ too. Now I have to write multithread application in C++. Which library for multithread programing would you recomend for me? if you know that I...
31
by: somebody | last post by:
No, I'm not a troll, just angry. I just started learning C++, and find it extremely lacking compared to Java. For example, I had to write my own functions to do something as simple as a case...
2
by: Markus Dehmann | last post by:
Is there a library that supports Java-like .properties files? I have several settings for each of my classes. So, I want to have a file like this: MainClass.numOfLoops = 3232 # the number of...
0
by: efiryago | last post by:
I am looking to implement some parallel data processing at the application level using java multithreading in a java stored procedure. >From what I learned in the DB2 doc, it seems to be achievable...
4
by: Le Tubs | last post by:
Hi I'm trying to find out some information about the following classes being thread safe (atomic), ifstream/ofstream. I have looked on google but have seemed to have gotten more confused before...
17
by: Allerdyce.John | last post by:
Hi, I am trying to compare the amount of work between using STL algorithm VS a plain Java loop. Let's say the class Rect has 2 attributes: area, and areaPerCent. In Java, I just write a...
6
by: Celldss | last post by:
I'm doing a standard AD query to retrieve a list of workstations that match certain criteria. I would like to iterate through that list and see if the workstations are still on the network and have...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.