Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include    <errno.h>
  2. #include    <stdio.h>
  3. #include    <unistd.h>
  4. #include    <sys/types.h>
  5.  
  6. #include "pa3.h"
  7. /* Test of concurrent access errors.                    */
  8. /* Two threads try to concurrently increment a counter.  Final counter  */
  9. /* value should be 2000, but it won't be.               */
  10.  
  11. int value = 0;
  12.  
  13. void
  14. worker_thread( void * ignore )
  15. {
  16.     int i;
  17.     int x;
  18.     for ( i = 0 ; i < 1000 ; i++ )
  19.     {
  20.         x = value;
  21.         sched_yield();
  22.         value = x + 1;
  23.     }
  24.     printf( "Worker thread %ld ends, value is %d.\n", thread_self(), value );
  25.     //return 0;
  26. }
  27.  
  28. int
  29. main()
  30. {
  31.     thread_t        ignore;
  32.  
  33.     printf( "Cogito ergo sum.\n" );
  34.    
  35.    
  36.     if ( thread_create( &ignore, worker_thread, (void*)ignore ) != 0 )
  37.     {
  38.         printf( "\x1b[2;31mpthread_create() failed file %s, line %d.\x1b[0m\n",
  39.             __FILE__, __LINE__ );
  40.         return 0;
  41.     }
  42.     else if ( thread_create( &ignore, worker_thread, (void*)ignore ) != 0 )
  43.     {
  44.         printf( "\x1b[2;31mpthread_create() failed file %s, line %d.\x1b[0m\n",
  45.             __FILE__, __LINE__ );
  46.         return 0;
  47.     }
  48.     else
  49.     {
  50.         while(1) { sched_yield(); }
  51.         //thread_exit(0);
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement