Do I need to join every topic in my application?

I'm new to multithreading, and I need to get the whole idea of ​​"join", and I need to combine every thread in my application and how does it work with multithreading?

+6
source share
5 answers

no, you can disconnect one thread if you want it to leave it alone. If you run a thread, either you disconnect it or attach it before the program terminates, otherwise this behavior is undefined.

To know that a thread needs to be separated, you need to ask yourself this question: "I want the thread to work after the main function of the program is completed?". Here are some examples:

  • When you do File / New, you create a new stream, and you disconnect it: the stream closes when the user closes the document. Here you do not need to join streams

  • When you do Monte Carlo simulations, some distributed computing, or any algorithms like Divide And Conquer, you run all the threads and you need to wait for all the results so you can combine them. Here you clearly need to join the stream before combining the results

+5
source

The pthread_join () function pauses the execution of the calling thread until the target thread terminates, unless the target thread has already terminated. When returning from a successful call to pthread_join () with the non-NULL value_ptr argument, the value passed to pthread_exit () on the terminating thread becomes available in the specified link by the value ofptr. When pthread_join () returns successfully, the target thread is terminated. The results of several simultaneous calls to pthread_join () defining the same target thread are undefined. If the calling thread pthread_join () is canceled, then the target thread will not be disconnected.

So pthread_join does two things:

  • Wait for the stream to finish.

  • Clear all associated resources with the stream.

This means that if you exit the process without calling pthread_join , then (2) the OS will execute for you (although it will not cancel the cancellation of the thread cleanup ) and (1) will not be executed.

So, do you need to call pthread_join , is it up to you (1).


Separate stream

If you do not need a thread to run, you can also pthread_detach it. A single thread cannot be joined (so you cannot wait for it to complete), but its resources are automatically freed if it terminates.

+2
source

Do I need to join every thread in my application?

Not necessarily - it depends on your design and OS. Join () is actively dangerous in GUI applications - i.e. If you do not need to know or do not care to find out if one thread has ended from another thread, you do not need to attach it.

I really try not to join / WaitFor any threads at all. Thread streams, application threads, and lifetimes often do not require any explicit termination - it depends on the OS and on whether the threads belong / or are explicitly associated with any resources that need to be explicitly terminated / closed / independently.

+1
source

Streams can be either joinable or disconnected. Separate flows should not be connected. On the other hand, if you did not join the merged stream, the application will leak some memory and some stream structures. C ++ 11 std :: thread will call std :: terminate if it has not been marked as separate and the thread object has gone out of scope without calling .join() . See pthread_detach and pthread_create . This is very similar to processes. When the baby comes out, he will remain a zombie until he waitpid . The resonance for this behavior is that the thread and the process creator may want to know the exit code.

Update: if pthread_create is called with an attribute attribute of NULL (default attributes are used), a connection thread is created. To create a separate thread, you can use the attributes:

 pthread_attr_t attrs; pthread_attr_init(&attrs); pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED); pthread_create(thread, attrs, callback, arg); 

Alternatively, you can disable the thread by calling pthread_detach on the created one. If you try to join a separate thread, pthread_join will return an EINVAL error code. glibc has a non-portable extension pthread_getattr_np , which allows you to get attributes of a working thread. This way you can check if the thread is disconnected using pthread_attr_getdetachstate .

+1
source

Do not join the stream, if not delete for all memory, you are new . It can be harmless, or it can be a bad habit.

A thread that you did not synchronize is in an unknown execution state. If it is a file write stream, it may be halfway through the file write, and then the application terminates. If this is a network communication stream, it could be halfway through a handshake.

The disadvantage of combining each thread is that one of them got into a bad state and is blocked, your application may freeze.

In general, you should try to send a message to your outstanding threads in order to tell them to go out and get out. Then you need to wait a little while for them to finish or otherwise answer that they know how to die, and then turn off the application. Now, before this, you must indicate that your program is no longer open for business - keep the windows of the graphical user interface, respond to requests from other processes that you are completing, etc. - therefore, if it takes longer than expected, the user is not worried. Finally, if everything goes wrong - if the threads refuse to respond to your request that they are closing, and you refuse them, then you should also log errors, so you can fix what might be a symptom of a more serious problem.

The last time a workflow hung unexpectedly, I initially thought it was a problem with a network outage and a timeout error. During a deeper check, this was due to the fact that before the shutdown synchronization, one of the used delete d objects was used: the undefined behavior, which turned out to be just like freezing in my propagation cases. If we were not careful, this error would be more difficult to track (now the right thing is to use a shared resource that we could not delete, but errors occur).

+1
source

All Articles