An idea for how to make a mutex without mutexes.
.gitignore | ||
compile-debug.sh | ||
compile-release.sh | ||
LICENSE | ||
main.cpp | ||
README.md | ||
test-sourceme.sh |
mutex idea
This is the implementation of an idea I had to make a mutex for thread synchronization without using CPU features (which is apparently what most mutex implementations use, or are supposed to use). As such, this sketch might have hidden limitations that I'm not aware of due to my little experience with parallel programming. If I find any, I'll note them somewhere in this document or fix them.
Does it work?
Yes! For this test case, at least. I don't know what use cases would break it. I verified that it works like it should by running it 10,000+ times on my machine.
How?
- A thread manager sets up, spawns, and manages some threads
- Threads ask for the mutex by setting a bool in the thread manager
- The thread manager constantly checks for threads that want the mutex
- It gives it to them when no threads are using it
- Threads tell the thread manager when they are done with the mutex
- Before asking for it again, they wait for the thread manager to tell them they've transferred it
TODO's
- make into a c++ class
- make an interface for the thread so you can use it to parallelize things without having to write a thread task function
- benchmark against a "normal" mutex
- execution time
- cpu usage
- memory usage