Question: I've made a hand-made mutex for my project, but I doubt if it is thread safe...

```
    bool blocked;

    while ( blocked )
    {

    }

    blocked = true;
    ...
    blocked = false;

```

Lets say, thread A passes the while loop and doesn't block the flag in time (doesn't have time to set the flag to `false`) and thread B passes the while loop too!

1. Is it possible? Why?
2. As I get it mutex has the very same principle of work. Why can not this occur in mutex? I've read about atomic operations which can not be interrupted... So the `check-if-mutex-available` and `mutex-block` can not be interrupted, right?

Answer: 
1. It's possible because the processor can switch from the first thread to the second after entering the loop, but before locking the mutex.
2. It's possible because they use kernel-level operations to ensure that the thread is not switched until a certain operation is complete.

On Windows for instance, you could make a mutex look like [this](http://pastebin.com/n45zL3sC).
Yes, the situation you described can happen. The reason for this is that the thread can be interrupted between testing `blocked` is `false`, and setting `blocked` to `true`. To get the behaviour you want you will need to use or emulate an atomic **test-and-set** operation.

Further information on test-and-set can be found [here](http://en.wikipedia.org/wiki/Test-and-set).