Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include "pa3.h"
- /* Test of concurrent access errors. */
- /* Two threads try to concurrently increment a counter. Final counter */
- /* value should be 2000, but it won't be. */
- int value = 0;
- void
- worker_thread( void * ignore )
- {
- int i;
- int x;
- for ( i = 0 ; i < 1000 ; i++ )
- {
- x = value;
- sched_yield();
- value = x + 1;
- }
- printf( "Worker thread %ld ends, value is %d.\n", thread_self(), value );
- //return 0;
- }
- int
- main()
- {
- thread_t ignore;
- printf( "Cogito ergo sum.\n" );
- if ( thread_create( &ignore, worker_thread, (void*)ignore ) != 0 )
- {
- printf( "\x1b[2;31mpthread_create() failed file %s, line %d.\x1b[0m\n",
- __FILE__, __LINE__ );
- return 0;
- }
- else if ( thread_create( &ignore, worker_thread, (void*)ignore ) != 0 )
- {
- printf( "\x1b[2;31mpthread_create() failed file %s, line %d.\x1b[0m\n",
- __FILE__, __LINE__ );
- return 0;
- }
- else
- {
- while(1) { sched_yield(); }
- //thread_exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement