Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::thread constructor does not handle references without std::ref

Reply
Thread Tools

std::thread constructor does not handle references without std::ref

 
 
ivan.popivanov@gmail.com
Guest
Posts: n/a
 
      10-21-2012
If the thread function (the first argument to std::thread, threadMain below) takes a reference parameter, the code doesn't work as expected unless one calls the constructor with std::ref surrounding the variable (see below).

Is this "by design"? I am using g++ 4.6.3 on Ubuntu.

#include <iostream>
#include <thread>
#include <cstdint>
#include <vector>
#include <iterator>

int count = 0;

void threadMain( int threadId, int & counted )
{
while( count < 1000000 )
{
++count;
++counted;
}
}

int
main( )
{
int counted1 = 0;
int counted2 = 0;

// std::thread t1( threadMain, 1, std::ref( counted1 ) );
std::thread t1( threadMain, 1, counted1 );

t1.join();

std::cout << counted1 << ", " << counted2 << ", " << counted1 + counted2 << std::endl;

return 0 ;
}

// to build: g++ -O3 -std=c++0x -pthread -o test test6.C
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      10-22-2012
On Monday, 22 October 2012 01:50:52 UTC+3, (E-Mail Removed) wrote:
> If the thread function (the first argument to std::thread, threadMain below) takes a reference parameter, the code doesn't work as expected unless one calls the constructor with std::ref surrounding the variable (see below).
>
> Is this "by design"? I am using g++ 4.6.3 on Ubuntu.


Seems so. std::thread has template constructor. How it should deduce that you want to pass an argument by reference? Same like with std::bind ... you have to use std::ref to indicate reference argument.
 
Reply With Quote
 
 
 
 
Werner
Guest
Posts: n/a
 
      10-24-2012
On Monday, October 22, 2012 12:50:52 AM UTC+2, (E-Mail Removed) wrote:
> If the thread function (the first argument to std::thread, threadMain below) takes a reference parameter, the code doesn't work as expected unless one calls the constructor with std::ref surrounding the variable (see below).
>
>
>
> Is this "by design"? I am using g++ 4.6.3 on Ubuntu.
>
>
>
> #include <iostream>
>
> #include <thread>
>
> #include <cstdint>
>
> #include <vector>
>
> #include <iterator>
>
>
>
> int count = 0;
>
>
>
> void threadMain( int threadId, int & counted )
>
> {
>
> while( count < 1000000 )
>
> {
>
> ++count;
>
> ++counted;
>
> }
>
> }
>
>
>
> int
>
> main( )
>
> {
>
> int counted1 = 0;
>
> int counted2 = 0;
>
>
>
> // std::thread t1( threadMain, 1, std::ref( counted1 ) );
>
> std::thread t1( threadMain, 1, counted1 );
>
>
>
> t1.join();
>
>
>
> std::cout << counted1 << ", " << counted2 << ", " << counted1 + counted2 << std::endl;
>
>
>
> return 0 ;
>
> }
>
>
>
> // to build: g++ -O3 -std=c++0x -pthread -o test test6.C


Generally, for threads it is safer to accept arguments by
value than by reference. The possibility always exists
that the spawning thread doesn't exist when the spawned thread
starts. Hence, it is better that the user makes explicit when
he wants it to be a reference (else what is referred to might
be gone...).

Hope this makes sense.

Regards,

Werner

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does C++ allow a constructor to call another constructor of the sameclass? Warren Tang C++ 9 04-15-2008 01:03 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
Possible to handle web requests without an ASPX page? i.e. have DLL handle request. jdlwright@shaw.ca ASP .Net 2 05-31-2005 05:42 PM
how to handle command line output(not terminal handle) Leon Python 2 11-04-2004 05:16 AM



Advertisments