Segment error caused by pthread_create and pthread_create
Generally, the end of a thread is ended by other threads in the process, and pthread_cancel is called.
However, we need to consider the nature of the terminated thread. On the one hand, the thread can be terminated or cannot be terminated, that is, the signal should not be sounded; on the other hand, if the thread can be terminated, there are two methods to end the process. One is synchronization. When the thread receives the signal, it suspends first and then responds to the signal when the next cancellation point is reached. The other is asynchronous, when this signal is received, the thread ends immediately.
Note: by default, a thread can be canceled and synchronization ends.
What is a cancellation point?
Cancellation points are many system calls, pthread_join, write, and many other system calls, including many library function calls and printf, because printf includes the previously mentioned System Call write.
I think I can do a simple test by myself.
Why do we need to consider the nature of the thread?
Consider the following:
Parent thread A and child thread B (asynchronous termination not set), access resource C together, parent thread ends child thread B, but pthread_cancel only sends A signal, then parent thread releases resource C, when B resumes running, it waits for the next cancellation point because it does not end directly. Before the next cancellation point, B accesses resource C, and the program crashes.
Therefore, the synchronization between threads either sets the asynchronous end of the subthread or the signal that the parent thread waits for the end of the subthread.
Appendix:
Set the interface for Synchronous end or asynchronous end of a thread:
Int pthread_setcanceltype (int type, int * oldtype );
Asynchronous PTHREAD_CANCEL_ASYNCHRONOUS synchronous PTHREAD_CANCEL_DEFERRED oldtype returns the original attribute
.