|
|
Create thread n° 0 Create thread n° 1 Execution of my_thread. Argument passed = 1 Create thread n° 2 Execution of my_thread. Argument passed = 2 Create thread n° 3 Execution of my_thread. Argument passed = 3 Create thread n° 4 Execution of my_thread. Argument passed = 4 Create thread n° 5 Execution of my_thread. Argument passed = 5 Create thread n° 6 Execution of my_thread. Argument passed = 6 Create thread n° 7 Execution of my_thread. Argument passed = 7 Execution of my_thread. Argument passed = 8 Thread 0 finished right The function called in the thread n° 0 finished right Thread 1 finished right The function called in the thread n° 1 finished right Thread 2 finished right The function called in the thread n° 2 finished right Thread 3 finished right The function called in the thread n° 3 finished right Thread 4 finished right The function called in the thread n° 4 finished right Thread 5 finished right The function called in the thread n° 5 finished right Thread 6 finished right The function called in the thread n° 6 finished right Thread 7 finished right The function called in the thread n° 7 finished right |
i++
in the for loop between the creation of the thread and the initialization of the arguments of my_thread.1) Do I misuse pthread ? |
2) There exist several thread libraries like Boost or ThreadJack. |
Argument passing is perfectly safe, as long as the parameters you're passing aren't currently being modified by other threads. |
By the time any of the threads begin execution, i might be long destroyed. |
If you want the calls to be performed sequentially, you should call my_thread regularly and not use threads. |
|
|
Create thread n° 0 Create thread n° 1 Create thread n° 2 Create thread n° 3 Execution of my_thread. Argument passed = 1 Create thread n° 4 Execution of my_thread. Argument passed = 0 Execution of my_thread. Argument passed = 4 Execution of my_thread. Argument passed = 2 Execution of my_thread. Argument passed = 3 Create thread n° 5 Create thread n° 6 Execution of my_thread. Argument passed = 5 Create thread n° 7 Execution of my_thread. Argument passed = 6 Execution of my_thread. Argument passed = 7 Thread 0 finished right Thread 1 finished right Thread 2 finished right Thread 3 finished right Thread 4 finished right Thread 5 finished right Thread 6 finished right Thread 7 finished right |
cout <<
either, it is guaranteed to be thread-safe, but you have to send your entire string at once, with just one operator<<. Or use a lock_guardBut i is not destroyed before all the threads are terminated since I wait for all of them in main with join. |
It is. i is destroyed when your thread creation loop ends and no longer exists when the threads are being joined. |
Same using C++ threads. On linux, they are available with both GCC and Clang compilers. |
request for member ‘emplace_back’ in ‘thread’, which is of non-class type ‘std::thread [8]’ |
C++ threads pass arguments to threads by value (by default) |
lalebarde wrote: |
---|
request for member ‘emplace_back’ in ‘thread’, which is of non-class type ‘std::thread [8]’ |
what version of gcc at least is required and with which options ? |
Sounds like you attempted to use an array instead of a vector. |
vector<thread> thread;
, it works. But if I declare it along with the size of the vector vector<thread> thread(8);
, what should be correct, it fails to compile : g++ -I"/home/alain/Documents/Poker/WorkSpace/MyUtils" -O0 -g3 -Wall -c -fmessage-length=0 -std=c++0x -std=gnu++0x -MMD -MP -MF"src/test.d" -MT"src/test.d" -o"src/test.o" "../src/test.cpp" In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/vector:63, from /home/lalebarde/Documents/Project/WorkSpace/MyUtils/myUtils.h:15, from ../src/test.cpp:15: /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/thread: In function ‘void std::_Construct(_T1*, const _T2&) [with _T1 = std::thread, _T2 = std::thread]’: /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/bits/stl_uninitialized.h:187: instantiated from ‘static void std::__uninitialized_fill_n<<anonymous> >::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = std::thread*, _Size = long unsigned int, _Tp = std::thread, bool <anonymous> = false]’ BTW, do you have a (some) good tutorial/reference link(s) for c++ thread /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/bits/stl_uninitialized.h:223: instantiated from ‘void std::uninitialized_fill_n(_ForwardIterator, _Size, const _Tp&) [with _ForwardIterator = std::thread*, _Size = long unsigned int, _Tp = std::thread]’ /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/bits/stl_uninitialized.h:318: instantiated from ‘void std::__uninitialized_fill_n_a(_ForwardIterator, _Size, const _Tp&, std::allocator<_Tp2>&) [with _ForwardIterator = std::thread*, _Size = long unsigned int, _Tp = std::thread, _Tp2 = std::thread]’ /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/bits/stl_vector.h:1035: instantiated from ‘void std::vector<_Tp, _Alloc>::_M_fill_initialize(size_t, const _Tp&) [with _Tp = std::thread, _Alloc = std::allocator<std::thread>]’ /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/bits/stl_vector.h:230: instantiated from ‘std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = std::thread, _Alloc = std::allocator<std::thread>]’ ../src/test.cpp:235: instantiated from here /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/thread:122: erreur: deleted function ‘std::thread::thread(const std::thread&)’ /usr/lib/gcc/x86_64-pc-linux-gnu/4.4.5/include/g++-v4/bits/stl_construct.h:74: erreur: used here make: *** [src/test.o] Erreur 1 |
erreur: deleted function ‘std::thread::thread(const std::thread&)’ |
thread(const thread&) = delete;