线程互斥锁相关程序《转载》

#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\n", 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\n", 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;
}

posted @ 2015-04-01 09:21  小子耳东  阅读(135)  评论(0编辑  收藏  举报