• Advertisement
Sign in to follow this  

Multiple Process Shared Resource

This topic is 2598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone, I'm quite new to coding with multiple processes and I have a question about preventing two processes from accessing the same resource at the same time. I assumed that you'll need to lock the shared resource while 1 process accesses it and then unlock it and allow the next one in.
I've wrote a quick class to handle this and was hoping someone would take a look to make sure I haven't done something stupid.


class GateKeeper
{
public:
GateKeeper()
{
CurrentKey = 0;
NextKey = 0;
Locked = false;
}
void Lock()
{
int MyKey = NextKey++;
while( ( MyKey != CurrentKey ) && Locked )
{
//Do Nothing
}
Locked = true;
}
void Unlock()
{
CurrentKey ++;
Locked = false;
}
int CurrentKey;
int NextKey;
bool Locked;
};
void main ()
{
GateKeeper gK;
}
void ThreadOne()
{
gK.Lock();
//Access Data Here
gK.Unlock();
}
void ThreadTwo()
{
gK.Lock();
//Access Data Here
gK.Unlock();
}


Any advice would be great!

Thanks in advance!
David

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement