Mutual Exclusion Locks
Can anyone explain what exactly is a mutual exclusion lock and how does one code/use one? Writing a DLL for a multithreaded C program that passes in a void **object which is supposed to point to a MUX. I'm not even sure where to begin.
I am very familiar with POSIX threads, and can offer you a general definition of a mutex (it appears you are using WinThreads).
A mutex (mutual exclusion) is a special type of Dijkstra semaphore that allows multiple threads to coordinate access to a shared resource.
Often in multi-threaded programming, it is desirable for a shared resource to be manipulated by just one thread at a time. The thread will attempt to obtain (lock) the mutex associated with the resource, and will block until it either locks the mutex or, optionally, times out while waiting for the mutex (it is also possible to 'try' a mutex without blocking).
A thread will be blocked by a mutex if another thread already has that mutex locked. Once the owning thread releases the mutex, any thread waiting on the mutex will automatically attempt to claim it. Once a thread has locked the mutex, it is free to manipulate the data the mutex protects, and must release the mutex when it is done.
It is possible to write threaded applications without mutexes, but this is rare. Mutexes can become a bottleneck in your code, since they serialize portions of your multi-threaded program.
Thank you for the reply. It makes sense. I remember going over that stuff in college but it's been a while. I'm assuming when you have data to protect in a C program the best way to do it is through the use of a struct? Then I guess you just reference that struct for whichever thread instance you are using.
Am I even close here?
Yes, in many cases, it makes a lot of sense to group the protected data and mutex in a struct together.