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

I

ivan.popivanov

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
 
Ö

Öö Tiib

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.
 
W

Werner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top