473,804 Members | 3,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

boost threads return type operator()

I've been working with boost::threads to do some multithreading in my
code and have run into some questions that I haven't been able to find
answers to.

I'll include some sample code to illustrate my questions.

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

using namespace std;

class mt
{
private:
double _alpha;
double _beta;
double _x;
double& _y1;

public:
double _y2;

public:
mt( double alpha, double beta, double x, double& y1 ):
_alpha(alpha), _beta(beta),
_x(x), _y1(y1) {};

void operator()()
{
// Some function more complicated than this...
_y1 = _alpha/_beta*_x;
_y2 = _y1;
};

double GetY2() { return _y2; };
};

void main()
{
double y1;
double y2;
mt a( 4,1,3,y1 );
boost::thread thrd( a );
thrd.join();
y2 = a.GetY2();
cout << y1 << endl;
cout << y2 << endl;
}

If I understand everything correctly, when I call
boost::thread thrd( a ),
it creates a new (thread local) instance of mt with which to work with
which goes out of scope at
thrd.join(). That is why y1 gives a good answer, but y2 contains garbage.

Now for my question:

Is it possible to call something like:
y = boost::thread thrd( a(x) );
where I've redefines operator() to take a double "x"
and return a double "y".
Then I don't have to give x and y when I construct "a".

Basically, my question is how to return something from the () operator
inside the thread.

Please ask for clarification if I haven't been clear.
Apr 18 '07 #1
1 3478
Chris Roth wrote:
I've been working with boost::threads to do some multithreading in my
code and have run into some questions that I haven't been able to find
answers to.
I'll include some sample code to illustrate my questions.
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
class mt
{
private:
double _alpha;
double _beta;
double _x;
double& _y1;

public:
double _y2;

public:
mt( double alpha, double beta, double x, double& y1 ):
_alpha(alpha), _beta(beta),
_x(x), _y1(y1) {};
void operator()()
{
// Some function more complicated than this...
_y1 = _alpha/_beta*_x;
_y2 = _y1;
};
double GetY2() { return _y2; };
};
void main()
Just a nit, but should be "int".
{
double y1;
double y2;
mt a( 4,1,3,y1 );
boost::thread thrd( a );
thrd.join();
y2 = a.GetY2();
cout << y1 << endl;
cout << y2 << endl;
}
If I understand everything correctly, when I call
boost::thread thrd( a ),
it creates a new (thread local) instance of mt with which to work with
which goes out of scope at
thrd.join().
Not quite. It doesn't go out of scope until you leave main.
All the join does is block the calling thread until the other
thread finishes.
That is why y1 gives a good answer, but y2 contains garbage.
No. You're overlooking the fact that the new thread is started
with a *copy* of your functional object. (Think of what would
happen otherwise if your functional object were a temporary,
which is often the case.) y1 has a good value because the
functional object contained a reference; all of the copies use
the same actual variable. y2 contains garbage because it was
never initialized in the original object; the child thread wrote
to a copy.

In general, the functional object should use only references or
pointers for out and inout values. If you change _y2 to a
reference in your class mt, you can then write:

double y1 ;
double y2 ;
boost::thread thrd( mt( 4, 1, 3, y1, y2 ) ) ;
// Obviously, you have to initialize the
// reference...
// Note that any use of y1 or y2 here is undefined
// behavior.
thrd.join() ;
std::cout << y1 << std::endl ;
std::cout << y2 << std::endl ;
Now for my question:
Is it possible to call something like:
y = boost::thread thrd( a(x) );
where I've redefines operator() to take a double "x"
and return a double "y".
Then I don't have to give x and y when I construct "a".
No. boost::thread always treats the functional object as
returning void.
Basically, my question is how to return something from the () operator
inside the thread.
You can't, per se. Boost.threads makes no provision for return
values or exceptions. On the other hand, it probably wouldn't be
too difficult to create a wrapper which would allow it (return
values, that is; exceptions would be considerably harder).

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

Apr 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2943
by: P G | last post by:
I hope this is on topic here. I have a problem compiling a simple example of the use of boost::bind. Please take a look at the program below. /**************************************************************************/ #include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp>
1
7793
by: Hardy | last post by:
Hi, just come into the boost world. just the first.cpp in the program_options examples, with many link error... devc++4.9.9.2, gcc 3.4.2, can I get your opinions on this problem? thank you~ make.exe -f "D:\temp\Makefile.win" all g++.exe -c main.cpp -o
0
1781
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different implementations of a member function of A that takes as an argument the abstract class Base (see Option 1 / Option 2): - Both compile without problems - Only the second works in python (see python program below).
1
2707
by: flopbucket | last post by:
Hi, After reading a bit about boost::lambda, I became curious how they implemented it. I downloaded it and had a look, but the all the headers and multiple templates make it a bit difficult to follow in a short time (I only spent maybe 15 minutes). Anyway, I decided to try some ideas out and came up with the following basic example: (obviously this is very far from boost::lambda and is special for streams, etc., but just trying to...
2
2909
by: toton | last post by:
Hi, I am trying to use boost::range with one of my own container class, and having some problem. I am missing some usage of range. Can anyone suggest a proper way for it ? To show the problem I am facing, giving a small test program to demonstrate . typedef vector<intVI; VI v; //vector of int.
1
5561
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
6
2551
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using boost::lexical_cast to cast a vector of strings to a string. The compiler I'm using is MSVC++ 2005 Express Edition. The gc++
2
2414
by: ironpingwin | last post by:
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?
19
3403
by: =?ISO-8859-1?Q?Nordl=F6w?= | last post by:
I am currently designing a synchronized queue used to communicate between threads. Is the code given below a good solution? Am I using mutex lock/unlock more than needed? Are there any resources out there on the Internet on how to design *thread-safe* *efficient* data- structures? /Nordlöw
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.