GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Mar-2008, 18:12
r2034kimo r2034kimo is offline
New Member
 
Join Date: Mar 2008
Posts: 1
r2034kimo is on a distinguished road

I NEED HELP WITH synchronization issues


CPP / C++ / C Code:
#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 ?
Last edited by LuciWiz : 02-Mar-2008 at 06:54. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault issues with 'realloc' aijazbaig1 C Programming Language 3 01-Feb-2008 15:10
Windows Updates and Secure Website Issues LocalTech Computer Software Forum - Windows 0 31-Jul-2007 05:30
Graphics Issues (Screenshots Included) NovaTiger Computer Hardware Forum 2 03-Apr-2007 18:16
Streamyx (DSL) and Windows ME issues. JdS Computer Software Forum - Windows 4 03-Apr-2004 05:15
Downloadable document compatibilty issues? rhino1616 Web Design Forum 3 07-May-2003 04:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:21.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.