#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int count = 0;
void* function1( void* arg )
{
int tmp = 0;
while( 1 ) {
pthread_mutex_lock( &mutex );
tmp = count++;
pthread_mutex_unlock( &mutex );
printf( "Count is %d
", tmp );
/* snooze for 1 second */
sleep( 1 );
}
return 0;
}
void* function2( void* arg )
{
int tmp = 0;
while( 1 ) {
pthread_mutex_lock( &mutex );
tmp = count--;
pthread_mutex_unlock( &mutex );
printf( "** Count is %d
", tmp );
/* snooze for 2 seconds */
sleep( 2 );
}
return 0;
}
int main( void )
{
pthread_create( NULL, NULL, &function1, NULL );
pthread_create( NULL, NULL, &function2, NULL );
/* Let the threads run for 60 seconds. */
sleep( 60 );
return 0;
}