| ▲ | Spinning around: Please don't – Common problems with spin locks(siliceum.com) | ||||||||||||||||||||||||||||||||||||||||
| 30 points by bdash 4 hours ago | 9 comments | |||||||||||||||||||||||||||||||||||||||||
| ▲ | jcranmer 30 minutes ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||
The basic rule of writing your own cross-thread datastructures like mutexes or condition variables is... don't, unless you have very good reason not to. If you're in that rare circumstance where you know the library you're using isn't viable for some reason, then the next best rule is to use your OS's version of a futex as the atomic primitive, since it's going to solve most of the pitfalls for you automatically. The only time I've manually written my own spin lock was when I had to coordinate between two different threads, one of which was running 16-bit code, so using any library was out of the question, and even relying on syscalls was sketchy because making sure the 16-bit code is in the right state to call a syscall itself is tricky. Although in this case, since I didn't need to care about things like fairness (only two threads are involved), the spinlock core ended up being simple: | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| ▲ | CamperBob2 an hour ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
Sheesh. Can something this complicated ever truly be said to work? | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| ▲ | gafferongames an hour ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||
Great article! Thanks for posting this. | |||||||||||||||||||||||||||||||||||||||||