I can't answer the question on Viruses but:
A mutex is an mechanism for stopping multiple threads accessing the same resource at the same time, or for synchronising threads.
For example if you have a printer and 2 threads try to print at the same time your will get a mix of the 2 outputs.
To solve this the threads request lock on the mutex object, ONLY ONE of the threads will be given the lock while the other will be put to sleep until the mutex is released.
We now have one thread running and one sleeping therefore we are guaranteed exclusive access to the printer, and our page prints.
Once the thread has finished with the printer it unlocks the mutex. The act of unlocking will wake the waiting thread(s) one of which will get the lock allowing it to continue with exclusive access to the printer.
I used a printer as an example but in reality you can use mutex objects to protect anything.
A mutex will only allow a single thread to lock it at anyone time, all other threads attempting access will sleep.
The mutex object itself can be thought of, in simple terms, as a memory location with a number it in.
This number starts as 1 when a thread tries to lock the mutex; if the number is 1 it sets it to 0 and continues, if however the number is 0 then the thread blocks (sleeps) waiting for the number to change back to 1.
When a thread no loner needs the mutex and unlocks it it returns the number to a 1 to indicate to the other threads that the mutex is no longer locked.
The other mechanism you need to know about is a semaphore, it is almost identical to a mutex except for the fact that it will allow a predetermined number of threads to lock it simultaneously. If you set this number to 1 then the semaphore acts in the same way as a mutex.
If you go to Google and search on POSIX threads there is a ton of information on all of this with examples, the theory between Windows and UNIX threads is near identical, just the system calls vary.
|