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).
You've pretty much got it already.

1. Yes, it's very possible. For a single core, threads get executed by the OS via [timeslicing](http://en.wikipedia.org/wiki/Preemption_%28computing%29#Time_slice). It runs thread A for a bit, then pauses it, and runs thread B for a bit. Thread A could get paused right after passing the while loop.
2. To solve problems like these, CPU's have implemented special instructions that CANNOT be interrupted by anything. These atomic operations are used by a mutex to check the flag, and set it in one operation.