We normalize all the behavior of the cars so they’re turning the same direction, and then it is up to each individual car to ensure there’s not another vehicle coming when they enter the roundabout.Įxact same problem, totally different solution. We can put a stoplight at the intersection (our lock) and this will regulate who can use the resource and when.Ī way to make a lockless intersection would require that we get rid of the stoplight, but also that we change the structure of the intersection. If two cars try to use this resource at the same time, they will crash. ![]() There is a space where cars going north and south need to intersect cars going east and west. Let’s look back at a real world example of locks, an intersection. ![]() They’re both based on a linked list, but you’ll notice that this one does not have any mutex or condition variable. Future versions of that lib may change.īoth libraries are very similar, they both have a constructor, a push method, and a pop method. This post will be using examples from this commit of lfqueue. Now that you’re up to speed, we’ll look at someone else’s code. Prepare to unlock your imagination!įirst off, if you haven’t read my Mediations on Writing a Queue, go do that first. We’ll talk about atomicity and the tradeoffs when choosing one strategy or the other, and end with some ways to write “lock-free” Ruby code. Why would we want to get rid of locks then? In this post I’ll revisit the queue that I wrote in C, and instead look at a “lockless” queue implementation. ![]() In programming locks keep multi-threaded programs honest by ensuring only one thread can access a resource at a time. It’s said that locks keep honest people honest. The rest of the analysis is still valid and hopefully useful to you, just know there’s actually more that needs to be done, don’t try to use that code for a mission critical application out of the box. Update: I did mention that lock free data structures are really hard to write, it looks like there might be some issues that haven’t been addressed in the implementation of this LF Queue that we’re referencing.
0 Comments
Leave a Reply. |