Aug 25, 2013 at 4:08pm UTC
You have to remember that this 'thread' constructor is a function of sorts itself, so if the ".detach()" member function returns a copy of the object then that copy is what gets forwarded to your thread and passed by reference.
Aug 27, 2013 at 11:49am UTC
I have used that, but it still didnt passed it by ref...
Aug 27, 2013 at 12:27pm UTC
> but it still didnt passed it by ref...
Before you jump to conclusions, check it out by writing a small test program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <vector>
#include <thread>
#include <memory>
#include <functional>
void thread_fun( std::vector<int >& seq )
{
std::cout << "thread: reference is to sequence at " << std::addressof(seq) << '\n' ;
seq.insert( seq.end(), { 5, 6, 7, 8, 9 } ) ;
}
#include <iostream>
int main()
{
std::vector<int > seq { 0, 1, 2, 3, 4 } ;
std::cout << "main: sequence is at " << std::addressof(seq) << '\n' ;
for ( int v : seq ) std::cout << v << ' ' ;
std::cout << '\n' ;
std::thread t( &thread_fun, std::ref(seq) ) ;
t.join() ;
for ( int v : seq ) std::cout << v << ' ' ;
std::cout << '\n' ;
}
Output:
main: sequence is at 0x22fee4
0 1 2 3 4
thread: reference is to sequence at 0x22fee4
0 1 2 3 4 5 6 7 8 9
Last edited on Aug 27, 2013 at 12:28pm UTC