#include <pthread.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define BUFFER_LENGTH 10 // Buffer length
#define DELAY_RUNTIME 10 // Run-time of program (in seconds)
#define DELAY_PRODUCER 100000 // Delay for producer thread (in microseconds)
#define DELAY_CONSUMER 200000 // Delay for producer thread (in microseconds)
#define DELAY_ARITHM 200 // Delay for arithmetic operations (in microseconds)
int in = 0; // First empty slot
int out = 0; // First non-empty slot
int counter = 0; // Number of items in queue
bool finish = false;
void print_queue( char *s ); // print the queue stored in the buffer
void *producer( void* ); // the producer thread
void *consumer( void* ); // the consumer thread
void inc( int &c ); // increment a variable
void dec( int &c ); // decrement a variable
int main( int argc, char *argv[] )
{
pthread_t tid_prod; /* the producer thread identifier */
pthread_t tid_cons; /* the consumer thread identifier */
pthread_attr_t attr; /* set of attributes for the thread */
/* get the default attributes */
pthread_attr_init( &attr );
print_queue( "strt" );
/* create the thread */
pthread_create( &tid_prod, &attr, producer, NULL );
pthread_create( &tid_cons, &attr, consumer, NULL );
sleep( DELAY_RUNTIME );
finish = true;
usleep( 100 );
/* now wait for the thread to exit */
pthread_cancel( tid_prod );
pthread_cancel( tid_cons );
return 0;
} // main
void print_queue( char *s )
{
int i;
cout << s << ". queue: ";
/*if( in == out )
{
for( i = 0; i < BUFFER_LENGTH; i++ )
cout << ".";
}
else */if( out < in || counter == 0 )
{
for( i = 0; i < out; ++i )
cout << ".";
for( i = out; i < in; ++i )
cout << "X";
for( i = in; i < BUFFER_LENGTH; ++i )
cout << ".";
}
else
{
for( i = 0; i < in; ++i )
cout << "X";
for( i = in; i < out; ++i )
cout << ".";
for( i = out; i < BUFFER_LENGTH; ++i )
cout << "X";
}
cout << " in: " << in;
cout << " out: " << out;
cout << " nr of items: " << (in + BUFFER_LENGTH - out) % BUFFER_LENGTH;
cout << " counter: " << counter;
cout << endl;
} // print_queue
void *producer( void* arg )
{
while( !finish )
{
while( counter == BUFFER_LENGTH )
{} // wait
in = (in + 1) % BUFFER_LENGTH;
inc( counter );
print_queue( "prod" );
usleep( DELAY_PRODUCER );
}
pthread_exit( 0 );
} // producer
void *consumer( void *arg )
{
while( !finish )
{
while( counter == 0 )
{} // wait
out = (out + 1) % BUFFER_LENGTH;
dec( counter );
print_queue( "prod" );
print_queue( "cons" );
usleep( DELAY_CONSUMER );
}
pthread_exit( 0 );
} // consumer
void inc( int &c )
{
int tmp;
tmp = c;
tmp++;
usleep( DELAY_ARITHM );
c = tmp;
}
void dec( int &c )
{
int tmp;
tmp = c;
tmp--;
usleep( DELAY_ARITHM );
c = tmp;
}
somebody help me to understand this program, it is a section teach synchronization processes, and pleace help me, because i don't understand,
could you please tell me what is code doing? and why counter does not contain accurate values? if I add synchronization code to enforce exclusive access to the variable counter,
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock( &mutex );
pthread_mutex_unlock( &mutex );
how to do it ?