Click to See Complete Forum and Search --> : sychronize process using pipe?


saipkjai
February 20th, 2006, 05:12 AM
can pipe sychronize thread or process? because I'm trying to create 5 thread or process that can take an integer value and display it. each time a thread display the value, it has to be decrement it by 1 until the value has reach 0. The problem that I'm having is how can that integer value be transfer to those 5 process or thread for those 5 thread needs to be running concurrently.Do I just fork() 5 thread first??..I'm really confuse on this...

I know this may sound like an homework question, in fact, it really is a homework question. But I have been doing research on the net and notes for 2 days and still have no idea how do u write an sychronize program using pipe...

sorry for my bad grammer..

Siddhartha
February 20th, 2006, 02:01 PM
The problem that I'm having is how can that integer value be transfer to those 5 process or thread for those 5 thread needs to be running concurrentlyInstead of passing the integer around, let it stay in it's place and guard access to it such that it's value can be modified by only one thread at a time.

Something like -
class CMyDecrementingClass
{
public:
void DecrementInteger ()
{
// Enable Thread synchronization object (say semaphore or critical section) here

if (m_nInteger > 0)
--m_nInteger;

// Disable the sync-object here
}

int GetInteger ()
{
return m_nInteger;
}

private:

int m_nInteger;
};
Inside the thread function -
// Pass a pointer to an object of the above class as a parameter
ThreadFunc (void* pParameter)
{
CMyDecrementingClass * pDecrementingAlgo = (CMyDecrementingClass*) pParameter;

pDecrementingAlgo->DecrementInteger ();
}
...Most of this is pseudo code. But, I hope you got the point.
Don't move the integer around!

In fact, for a start you can ignore the semaphore and it would still work fine.