Segment error caused by pthread_create and pthread_create

Source: Internet
Author: User

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

.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.