Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does std::thread.join() do?

By definition from C++ reference:

Blocks the current thread until the thread identified by *this finishes its execution.

So does this mean when using .join(), there's no need to mutex.lock() when that thread calls some function? I'm new to mutual exclusion and threading, so I'm kind of confused.

Note: I've found a book C++ Concurrency in Action and I am reading the book. It is very well written for a beginner on multithreading like me.

Thank you all for the help.

like image 838
Guagua Avatar asked Mar 01 '13 00:03

Guagua


People also ask

What does join () do in C++?

Syntax of C++ thread join The C++ thread join is used to blocks the threads until the first thread execution process is completed on which particular join() method is called to avoid the misconceptions or errors in the code. If suppose we are not using any join() method in the C++ code.

Why do we need to join thread in C++?

Threads can be either joinable or detached. Detached threads should not be joined. On the other hand, if you didn't join the joinable thread, you app would leak some memory and some thread structures. c++11 std::thread would call std::terminate, if it wasn't marked detached and thread object went out of scope without .

What does it mean for a thread to join?

Joining a thread means to wait for it to complete. That is, block the current thread until another completes.

Is thread join necessary?

No join or System. exit necessary. Each thread lives its own life. As long as at least one thread is running, the program keeps running.


2 Answers

You still need mutexes and conditions. Joining a thread makes one thread of execution wait for another thread to finish running. You still need mutexes to protect shared resources. It allows main() in this example to wait for all threads to finish before quitting itself.

#include <iostream> #include <thread> #include <chrono> #include <mutex>  using namespace std;    int global_counter = 0; std::mutex counter_mutex;  void five_thread_fn(){     for(int i = 0; i<5; i++){         counter_mutex.lock();         global_counter++;         counter_mutex.unlock();         std::cout << "Updated from five_thread"  << endl;         std::this_thread::sleep_for(std::chrono::seconds(5));     }     //When this thread finishes we wait for it to join }  void ten_thread_fn(){     for(int i = 0; i<10; i++){         counter_mutex.lock();         global_counter++;         counter_mutex.unlock();         std::cout << "Updated from ten_thread"  << endl;         std::this_thread::sleep_for(std::chrono::seconds(1));     }     //When this thread finishes we wait for it to join } int main(int argc, char *argv[]) {     std::cout << "starting thread ten..." << std::endl;     std::thread ten_thread(ten_thread_fn);      std::cout << "Running ten thread" << endl;     std::thread five_thread(five_thread_fn);       ten_thread.join();     std::cout << "Ten Thread is done." << std::endl;     five_thread.join();     std::cout << "Five Thread is done." << std::endl; } 

Note that the output might look like this:

starting thread ten... Running ten thread Updated frUopmd atteend_ tfhrroema df ive_thread Updated from ten_thread Updated from ten_thread Updated from ten_thread Updated from ten_thread Updated from five_thread Updated from ten_thread Updated from ten_thread Updated from ten_thread Updated from ten_thread Updated from ten_thread Updated from five_thread Ten Thread is done. Updated from five_thread Updated from five_thread Five Thread is done. 

Since std::cout is a shared resource access and use of it should also be mutex protected too.

like image 172
Joel Avatar answered Oct 05 '22 13:10

Joel


join() stops current thread until another one finishes. mutex stops current thread until mutex owner releases it or locks right away if it isn't locked. So these guys are quite different

like image 20
event Avatar answered Oct 05 '22 11:10

event