473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 3459
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 objektorientierter Datenverarbeitung
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
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. ...
1
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~ ...
0
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...
1
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...
2
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...
1
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
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...
2
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...
19
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.