Hi, I am stuck at this multithreading thing -yes, its been about two and a half months and I am still here at the same place with no progress- I now have problem on mutual exclusion, more clearly, I am trying to avoid confusion when two threads call same function -so that they do not interfere with each other- certainly I need mutual exclusion, no I have two questions:
1) Do I have to add mutual exclusion code to all the functions that might be called by threads at the same time?
2) Can you give some hint on achieving this? ;D
Thanx... Unfortunately I could not get used to using the search function here, so I am sorry...
Mutual exclusion
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Mutual exclusion
you'll need to protect (through mutual exclusion) any piece of code that *must not* be called by several thread together. for instance, you clearly can't let 2 threads modify the "head of physical available pages list" in the mean time, or have one interrupted by another while it is modifying it.
Instead of protecting every call to the function like
you should push protection *within* the protected function:
So there's less risk you forget to protect one function than one function call.
Also remember to unlock stuff even in the case of unexpected event (and early procedure return).
Instead of protecting every call to the function like
Code: Select all
mutex.lock(memory);
x=memAlloc(size);
mutex.unlock(memory);
Code: Select all
addr_t* memAlloc(size_t s) {
mutex.lock(memory);
<alter state>
mutex.unlock(memory);
}
Also remember to unlock stuff even in the case of unexpected event (and early procedure return).