Sign in to follow this  
thekiwimaddog

Multiple Process Shared Resource

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this