Exit of Thread

Source: Internet
Author: User

2. Thread exit
2.1 normal Exit Processing of threads:

1. Handling of predictable errors: Methods for stopping the entire program
Set the global switch and use extern in the main process and thread to reference the switch. When the switch in the main process or thread changes and requires exit, all threads exit, the main process calls the function pthread_join () and waits for the exit of all threads in a blocking manner.

2. Handling of predictable errors: Methods for stopping the entire program
For the receiving thread: internal control information can be sent to exit the thread message to exit each receiving thread. [1. method is recommended]

2.2 thread Exit Processing:
1. The main process uses the pthread_cancel (pthread_t thread) function to exit the thread. It is equivalent to sending a signal to cause the thread to exit.
However, once the pthread_cancel function is used, the mutex lock must be considered, because a deadlock may occur.
When the pthread_mutex_lock function is called, The pthread_cancel function is used before pthread_mutex_unlock is called, resulting in a deadlock.
[Suggestion] do not use the pthread_cancel function when writing code.

**************************************** *************
In general, lock and unlock a piece of Running code, as shown below:
Pthread_mutex_lock (& mutex );

// Run the code;

Pthread_mutex_unlock (& mutex );

If an error occurs while running the code and an exception occurs, causing the thread to exit unexpectedly, what should I do? pthread_unlock is not called and the lock resource is not unlocked. You can modify it using the following method.

Pthread_cleanup_push (pthread_mutex_unlock, (void *) & mutex );

Pthread_mutex_lock (& mutex );

/* Do some work */

Pthread_mutex_unlock (& mutex );

Pthread_cleanup_pop (0 );

In this case, if the unlock function is not called when an error occurs in the running code, pthread_cleanup_up will be called automatically. If the parameter is 0, the push function will not be executed.

However, if an exception error occurs, this parameter does not affect the execution of the cleanup function when the exception is terminated.

It must be noted that if the thread is in the pthread_cancel_asynchronous state, the above Code segment may fail, because the cancel event may occur between pthread_cleanup_push () and pthread_mutex_lock (), or between pthread_mutex_unlock () and pthread_cleanup_pop (). This causes the unlock function to unlock a mutex variable that is not locked, causing an error. Therefore, when using the cleanup function, you should temporarily set it to the pthread_cancel_deferred mode. Therefore, the POSIX Linux implementation also provides a pair of pthread_cleanup_push_defer_np ()/pthread_cleanup_pop_defer_np () Extension functions which are equivalent to the following code segment:

{Int oldtype;

Pthread_setcanceltype (pthread_cancel_deferred, & oldtype );

Pthread_cleanup_push (routine, ARG );

...

Pthread_cleanup_pop (execute );

Pthread_setcanceltype (oldtype, null );

}

++ ++

Set exit type pthread_setcanceltype

# Include <pthread. h>

Int pthread_setcanceltype (INT type, int * oldtype );

Return Value: 0 is returned if the function is successful. Any other return values indicate errors.

Set the thread exit type to the latency or asynchronous type. The value of type is pthread_cancel_deferred or pthread_cancel_asynchronous.

When a thread is created, the default value is the delay type. In asynchronous mode, a thread can be exited at any time of execution.

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.