by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :)by MathLes and ClintCode AYYILDIZ TEAM :) Computer Engineering Tips - Computer Engineering news and articles - Thread Cancellation Computer Engineering Tips - Computer Engineering news and articles - Thread Cancellation
  Home arrow Operating Systems arrow Fundamentals arrow Thread Cancellation
Computer Engineering Tips - Computer Engineering news and articles | _DATE_FORMAT_LC
 
 
Main Menu
Home
News
Algorithms
Artificial Intelligence
Bioinformatics
Computer Graphics
Computer Networks
Computer Organization
Computer Security
Data Mining
Data Structures
Database Systems
Distributed Computing
Internet
Operating Systems
Parallel Computing
Programming Languages
Publications
Robotics
Software Engineering
Other Articles


Partners

All Partners

 
 
 
Thread Cancellation _CMN_EMAIL
_USER_RATING: / 0
_VOTE_POOR_VOTE_BEST 

Termination of a thread takes place when it exits normally either by calling pthread_exit or by returning from its thread function. However, it is possible for a thread to request that another thread terminates. This is called canceling of a thread.

pthread_cancel can be used to cancel a thread. User can pass the thread ID of the thread to be canceled. In order to free the resources a canceled thread should be joined. A special value given by PTHREAD_CANCELED is returned by the canceled thread.

Often a thread may be in some code that must be executed in an all-or-nothing fashion. For instance, the thread may allocate some resources, use them, and then de-allocate them. If the thread is cancelled in the middle of the code, it may not have the opportunity to de-allocate the resources, it is possible for a thread to control whether and when it can be cancelled.

A thread may be in one of the three states with regard to thread cancellation.

  • The thread may be asynchronously cancelable. The thread may be canceled at any point in its execution.
  • The thread may be synchronously cancelable. The thread may not be canceled at just any point in its execution. Instead thread should be cancelled at some specific points in its execution after queuing the cancellation requests.
  • A thread may be uncancelable. Attempts to cancel the thread are quietly ignored.

When initially created, a thread is synchronously cancelable.

Synchronous and Asynchronous Threads:

A thread cancelable asynchronously may be canceled at any point during its execution

Whereas threads canceled only at particular places in its execution constitute the synchronously cancelable thread. These places are called cancellation points. The thread will queue a cancellation request until it reaches the next cancellation point.

pthread_setcanceltype can be used in order to make a thread asynchronously cancelable. Due to this the thread actually calling the function gets affected. PTHREAD_CANCEL_ASYNCHRONOUS should be the first argument making the thread asynchronously cancelable. PTHREAD_CANCEL_DEFERRED can also be used returning the thread to the synchronously cancelable state. If not null, a pointer to a variable receiving previous cancellation type for the thread should be used as the second argument.This call, for example, makes the calling thread asynchronously cancelable.

pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHORONOUS, NULL);

What constitutes a cancellation point, and where should these be placed? The most direct way to create a cancellation point is to call pthread_testcancel. This does nothing except process a pending cancellation in a synchronously cancelable thread. For lengthy computation in a thread function user should use pthread_testcancel. This can easily be used at points where thread gets cancelled without any resource leak.

Certain other functions are implicitly cancellation points as well. These can be noted down from the man page of pthread_cancel. Other functions using these functions internally will thus indirectly behave as cancellation points.

Uncancelable Critical Sections:

pthread_setcancelstate function can be used to disable cancellation of thread itself. Although this also affects the calling thread like pthread_setcanceltype. PTHREAD_CANCEL_DISABLE and PTHREAD_CANCEL_ENABLE, being the first arguments are used to disable and re-enable cancellation and a variable receiving the previous cancellation state is being pointed to by the second argument. Thread cancellation is disabled in the calling thread by this call:

pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, NULL);

Using pthread_setcancelstate enables you to implement critical sections. A critical section is a sequence of code that must be executed either in its entirety or not at all; in other words, if a thread begins executing the critical section, it must continue until the end of the critical section without being canceled.

When to use thread cancellation:

In general, except in some unusual circumstances, ending the execution of a thread should not be done using thread cancellation mechanism. Rather giving an indication to the thread that it should exit, and then to wait for the thread to exit on its own in an orderly fashion is considered to be a better strategy.

 
 Sponsored Links

_USER_RATING: / 0
_VOTE_POOR_VOTE_BEST 
_ITEM_PREVIOUS   _ITEM_NEXT
 
       
         
     
 
Advertisement
 
   

Copyright 2005 - 2006 Science Tips Team. All rights reserved.

Nanotechnology Development Blog