Advertisement
Guest User

Untitled

a guest
Jun 4th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pthread_mutex_t reading_mutex = PTHREAD_MUTEX_INITIALIZER;
  2. //############################################################
  3. int main() {
  4. alive_threads = number_of_threads;
  5.     for (i = 0; i < thread_quantity; i++) {
  6.         pthread_create(&thread_id[i], NULL, thread_function, NULL)
  7.             created_threads++;
  8.     }
  9.     while (alive_threads) {
  10.         usleep(1000);
  11.     }
  12. }
  13. void clean(void *) {
  14.   alive_threads--;
  15. }
  16. void *thread_function(void *data) {
  17. while (1) {
  18.   pthread_cleanup_push(clean, NULL);
  19.     pthread_mutex_lock(&reading_mutex)
  20.     //some code...
  21.     pthread_mutex_unlock(&reading_mutex);
  22.     if (bytes_readed > 0) {
  23.            //no, don't need that code
  24.                
  25.     pthread_cleanup_pop(1);
  26.     pthread_cancel(pthread_self())
  27.     return data;
  28. }
  29. /////////////////////////////////////####################### test.txt #################
  30. The pthread_cleanup_push() function pushes routine onto the top of  the
  31.        stack  of clean-up handlers.  When routine is later invoked, it will be
  32.        given arg as its clean argument.
  33.        The pthread_cleanup_pop() function removes the routine at  the  top  of
  34.        the  stack  of clean-up handlers, and optionally executes it if execute
  35.        is nonzero.
  36.  
  37.        A cancellation clean-up handler is popped from the stack  and  executed
  38.        in the following circumstances:
  39.  
  40.        1. When  a thread is canceled, all of the stacked clean-up handlers are
  41.           popped and executed in the reverse of the order in which  they  were
  42.           pushed onto the stack.
  43.  
  44.        2. When  a  thread  terminates by calling pthread_exit(3), all clean-up
  45.           handlers are executed as described in the preceding point.   (Clean-
  46.           up  handlers are not called if the thread terminates by performing a
  47.           return from the thread start function.) clean
  48.  
  49.        3. When a thread calls pthread_cleanup_pop()  with  a  nonzero  execute
  50.           argument, the top-mst clean-up handler is popped and executed.
  51.  
  52.        POSIX.1  permits pthread_cleanup_push() and pthread_cleanup_pop() to be
  53.        implemented as macros that expand  to  text  containing  '{'  and  '}',
  54.        respectively.   For  this  reason, the caller must ensure that call
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement