Wednesday, May 4, 2011

pthread_cancel example c c++

NAME         top

       pthread_cancel - send a cancellation request to a thread

SYNOPSIS         top

       #include <pthread.h>

       int pthread_cancel(pthread_t thread);

       Compile and link with -pthread.

DESCRIPTION         top

       The pthread_cancel() function sends a cancellation request to the thread
       thread.  Whether and when the target thread reacts to the cancellation
       request depends on two attributes that are under the control of that
       thread: its cancelability state and type.

       A thread's cancelability state, determined by pthread_setcancelstate(3),
       can be enabled (the default for new threads) or disabled.  If a thread has
       disabled cancellation, then a cancellation request remains queued until the
       thread enables cancellation.  If a thread has enabled cancellation, then
       its cancelability type determines when cancellation occurs. (pthread_cancel)


       A thread's cancellation type, determined by pthread_setcanceltype(3), may
       be either asynchronous or deferred (the default for new threads).
       Asynchronous cancelability means that the thread can be canceled at any
       time (usually immediately, but the system does not guarantee this).
       Deferred cancelability means that cancellation will be delayed until the
       thread next calls a function that is a cancellation point.  A list of
       functions that are or may be cancellation points is provided in
       pthreads(7).

       When a cancellation requested is acted on, the following steps occur for
       thread (in this order):

       1. Cancellation clean-up handlers are popped (in the reverse of the order
          in which they were pushed) and called.  (See pthread_cleanup_push(3).)

       2. Thread-specific data destructors are called, in an unspecified order.
          (See pthread_key_create(3).)

       3. The thread is terminated.  (See pthread_exit(3).)

       The above steps happen asynchronously with respect to the pthread_cancel()
       call; the return status of pthread_cancel() merely informs the caller
       whether the cancellation request was successfully queued.

       After a canceled thread has terminated, a join with that thread using
       pthread_join(3) obtains PTHREAD_CANCELED as the thread's exit status.
       (Joining with a thread is the only way to know that cancellation has
       completed.)

RETURN VALUE         top

       On success, pthread_cancel() returns 0; on error, it returns a nonzero
       error number.

ERRORS         top

       ESRCH  No thread with the ID thread could be found.

 
Example 

#include <stdio.h> 
#include <string.h> 
#include <pthread.h> 
 
pthread_t threads[5]; 
int done[5]; 
 
void *thread_main(void *); 
 
int main(void) 
{ 
    int i; 
    int rc; 
    int status; 
     
    printf("pid=%d\n", getpid()); 
 
    for (i = 0; i < 5; i++) 
    {     
        done[i] = 0; 
        pthread_create(&threads[i], NULL, &thread_main, (void *)i); 
        printf("%d, %d\n", i, threads[i]); 
    } 
 
    for (i = 4; i >= 0; i--) 
    { 
        rc = pthread_cancel(threads[i]); // 
        if (rc == 0) 
        { 
            // 자동종료 
            rc = pthread_join(threads[i], (void **)&status); 
            if (rc == 0) 
            { 
                printf("Completed join with thread %d status= %d\n",i, status); 
            } 
            else 
            { 
                printf("ERROR; return code from pthread_join() is %d, thread %d\n", rc, i); 
                     return -1; 
            } 
        } 
    } 
 
    return 0; 
} 
 
void *thread_main(void *arg) 
{ 
    int i; 
    double result=0.0; 
 
    printf("therad: %d, %d\n", (int)arg, getpid()); 
 
    while (!done[(int)arg]) 
    { 
      for (i=0; i < 1000000; i++) 
        { 
          result = result + (double)random(); 
      } 
        printf("thread: %d, result = %e\n", (int)arg, result); 
    } 
 
    pthread_exit((void *) 0); 
}