The order that join is called isn't too important in this case. Joining is simply a blocking call that makes sure the thread completes before moving onward. It could be both threads had already completed their work before join was called for either. Your two threads are asynchronous and have no common thread. I suppose chances are f will happen before F but this should not be relied on.
The two threads are independent of each other - hence no assumptions should be made re the order these are executed - indeed for multi-core cpus they could be executed in parallel on different cores. Also note that output streams are not thread safe and if the same stream is used in multiple threads (like here for std:cout) then these operations should be synchronised.
Unless sync_with_stdio(false) has been issued, it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output. https://en.cppreference.com/w/cpp/io/cout
it is safe to concurrently access these objects from multiple threads for both formatted and unformatted output.
Hmmm. I've certainly had issues with using std::cout in different threads where output from the threads have been 'intermixed' without using a sync lock. If we have to use a stream in a thread, we put it in a MS 'critical section' (wed don't use C++ threads).
Is it possible you used chained statements like std::cout << "The answer is " << i <<".";
Even though it looks like a single print statement, this would be 3 separate insertion operations. Even if each stream operation is thread safe, there would be no such guarantee for the 3 of them together.
> If we have to use a stream in a thread, we put it in a MS 'critical section' (wed don't use C++ threads).
To make sure that the output would not be 'intermixed' (interleaved), use a synchronised output stream wrapper eg. std::osyncstream (defined in <syncstream>)
It provides the guarantee that all output made to the same final destination buffer (std::cout in the examples above) will be free of data races and will not be interleaved or garbled in any way, as long as every write to the that final destination buffer is made through (possibly different) instances of std::basic_osyncstream. https://en.cppreference.com/w/cpp/io/basic_osyncstream
> Even though it looks like a single print statement, this would be 3 separate insertion operations.
> Even if each stream operation is thread safe, there would be no such guarantee for the 3 of them together.
Yes. Even though it looks like a single print statement, it is not a single stream operation.
To make sure that the output would not be 'intermixed' (interleaved), use a synchronised output stream wrapper eg. std::osyncstream (defined in <syncstream>)
> And how to define it without reference, like a copy of the object?
By default, the constructor of the thread object copies (or moves) the arguments.
> Why do we need to define thread t1 with reference, ref(some_vec)?
We do it when we want the thread to get a reference to the original object (rather than a copy).
The arguments to the thread function are moved or copied by value. If a reference argument needs to be passed to the thread function, it has to be wrapped (e.g., with std::ref or std::cref). https://en.cppreference.com/w/cpp/thread/thread/thread#Notes
std::reference_wrapper:
It is frequently used as a mechanism to store references inside standard containers (like std::vector) which cannot normally hold references.
...
Helper functions std::ref and std::cref are often used to generate std::reference_wrapper objects.
... std::reference_wrapper is also used to pass objects by reference to std::bind, the constructor of std::thread, or the helper functions std::make_pair and std::make_tuple. https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper
Thank you for your answer, but how to define a thread to get a copy of the object to be sent to a function? When I define t1 the following way, I get errors! std::thread t1{ f, some_vec};
Severity Code Description Project File Line Suppression State
Error C2672 'invoke': no matching overloaded function found test_1 C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\thread 55
As well as, std::thread t1{ f(some_vec)}; doesn't work.