rwMakeThreadFunctions and Passing Parameters By-reference

Article ID: 929
Last updated: 01 Feb, 2008
Article ID: 929
Last updated: 01 Feb, 2008
Revision: 1
Views: 2890
Posted: 07 May, 1999
by Dean J.
Updated: 01 Feb, 2008
by Dean J.
Problem


I've created a thread object from a function which is passed a parameter by-reference, but the object in the thread appears to be a copy of the original, instead of a reference to it. What is causing this?


Cause


The rwMakeThreadFunction creates a thread object, that will internally call your function when it is run. When the thread object is taking in parameters, it doesn't know that the internal function is taking the parameter by-reference, and instead receives the parameter by-value. This new object is then passed by-reference to the underlying function, resulting in a copy of the original existing in the thread.


Action


The problem above is a limitation of the template based rwMakeThreadFunction calls, the macro based rwMakeThreadFunctions allow you specify the parameter types, including whether the parameter will be passed by-reference, or by-value. For instance, if you created a function:
void function(int& i) { /*** do something ***/ }
and then created a Thread object from that function:
rwMakeThreadFunction(function, j)
the instance i in the function invoked by the thread object will be a copy of j, instead of a reference to j. On the other hand, the macro function:
rwMakeThreadFunctionGA1(void, function, int&, j)
will correctly have the instance i in the function be a reference to j, instead of a copy of j. Details concerning the use of the macro based rwMake* functions can be found in the Threads.h++ Class Reference. This problem is also present in the other rwMake* functions, and the corresponding macro versions of those functions will also correct the issue.
This article was:   Helpful | Not helpful
Report an issue
Article ID: 929
Last updated: 01 Feb, 2008
Revision: 1
Views: 2890
Posted: 07 May, 1999 by Dean J.
Updated: 01 Feb, 2008 by Dean J.

Others in this category