The Swift post doesn't say whether, in fact, the popular C++ stdlib implementations do exhibit spurious failures for tryLock. That might vary by implementation and by platform.
I write C++ in accordance with the standard as much as possible. If the standard doesn't guarantee something, I try to avoid assuming it. Hence, I would write code which assumes tryLock might spuriously fail, even if some stdlib happens to not exhibit them.
A strong try_lock is a potentially "blocking" operation because it doesn't have an upper limit on the number of iterations. In practice an implementation would probably not fail after the first spurious LL/SC failure, but it may decide to fail after, say, 16 attempts. Or 16384 attempts.
There are a bunch of situations where weak try locks cause issues, most of them are around the assumption that if try lock fails its because something else holds the lock and is doing some shared work.
We had an initial try lock which was weak and ran into a bunch of odd issues because of this where people assumed try lock failing meant something else held a lock, it was just safer to make try lock be strong.
In my experience with spurious wakeups, code that is incorrect because of spurious wakeups is always also incorrect without them. That's because, even if the underlying kernel primitive doesn't wake up spuriously, there are preemption scenarios that nevertheless manifest on the application side exactly the same as a spurious wakeup.
I suspect that the case with spurious try_lock failures is similar, and that if code is incorrect because of spurious try_lock failures, it's also incorrect without them. Of course, the failures will be much more rare as the threads will need to be preempted at just the right times for bad things to happen.
I can easily imagine cases. Are you imagining that correct use of try_lock must involve a retry loop? Because if try_lock is strong, then I don't think that's the case.
For example, if a handful of threads each call try_lock on a common lock and do a bit of work if they acquire it, then someone who waits on all of them can safely assume the work is done if try_lock is strong and cannot safely assume this if it is weak.
Or a bunch of producer threads might wish to send a wake-up message to a consumer after finishing to notify it that one or more work items is done. It's okay to move on and assume someone else will notify the consumer if the try_lock call is strong, but unsafe if not.
std::lock will keep tryLocking until it succeeds. There just isn't that much use for trying to lock a mutex and then just shrugging and going on without that mutex.
It's not true that a spurious failure means you could have done useful work right now. In fact the opposite is probably true.
If I understand correctly spurious failures occur when the cache line was contended and your core lost exclusive ownership.
When they occur you're effectively saving a cache miss because otherwise the line has to be fetched directly from the core that now owns the line (or a shared cache), or at least invalidated everywhere else if it's in the shared and not invalid state.
If your atomic is properly isolated into its own cache line (no false sharing), and you have a bunch of threads spinning on CAS on that atomic , then by definition the only reason another core would have the line is because it intends to update it (and getting it back would cause ping pong), or has already updated it (meaning your CAS is going to fail anyway)
In that scenario a spurious failure is just a faster failure and it's an optimization, not a pessimization.
What is that else though? The most common use of try_lock is inside std::lock-like algorithms. A spurious failure in try_lock means that std::lock ends up doing lot more work, as it will unlock all previously locked mutexes and restart the locking.
This isn't about "why have try_lock", this is about the fact that if try_lock can spuriously fail and the caller cannot tell whether the failure was real or spurious, the most common user of try_lock is pessimized.
Do you have a real example of a system doing this? I've never seen try_lock in user code except for testing mutexes and in implementations of std::lock or similar protocols
The only reason for try_lock to exist at all is because sometimes, you want to see if it's possible to acquire a lock, and take a different branch if you can't.
I don't think I've ever used it in real code either, but I find it very hard to believe it's implemented in pthreads, C++ std mutexes, Java and various other languages for no reason at all.
I don't think I've written any production code that used semaphores either, I'm not spending my time asking Reddit whether they're really used anywhere.
This is a discussion about "should it have weak or strong semantics" and the answer to that is dependent on what the real world uses for the thing are. /u/SkoomaDentist seems to be claiming they've seen a real world use case I haven't seen before, which is why I'm interested and asking if they have a link.
I have done it and colleagues also used it. Mostly when implementing some sort of high contention queues. It's also useful, when you are implementing state machines and async locking over multiple threads or even in the same thread without a recursive mutex. Just drop back to the event loop.
Say you're implementing a memory allocator with multiple independent arenas to minimize contention. Unless you preallocate an arena per core (or some multiple of cores), you might want to dynamically grow the number of arenas in response to contention, to minimize fragmentation from unused arenas. If you're using locks to protect the arenas, you can call TryLock() to see if an arena is contended, and if it is, then move on to another arena, creating a new arena if they're all contended. (This is basically what glibc malloc now does.)
Another example is when you have some cooperative maintenance process (like periodically freeing unused resources) that should be single-threaded (and is therefore guarded by a lock), but the responsibility for the maintenance process is equally distributed among multiple threads. In that case, you can use TryLock() to determine if some other thread is already doing the work and just skip the maintenance path if so.
In the early 2000s we used win32 TryEnterCriticalSection in a realtime system where we knew there was low contention and it was acceptable for that realtime thread to simply skip the access occasionally if the mutex was already locked.
In general it can make it easier to implement situations where some object (or set of objects) is manipulated by one or more threads that need to lock the object for that duration and said duration can be very long (for whatever counts as very long) while another thread only requires read access to update its local state and does not want to wait for that duration. You could do that using lock free queues / mailboxes but that can end up being rather complex while tryLock allows using simpler traditional mutex based access.
That makes no difference for that scenario as long as those failures are rare and transient. Whether the mutex was actually already locked when tryLock fails is inconsequential as long as the failures stay rare enough.
Even putting aside the reason why spurious failures happen (there are some great comments here about that), by design such service is allowed to skip the work (need to handle starving somehow even without spurious failures) so it works as intended.
7
u/tialaramex Feb 19 '26
The Swift post doesn't say whether, in fact, the popular C++ stdlib implementations do exhibit spurious failures for
tryLock. That might vary by implementation and by platform.1
u/pdimov2 Feb 21 '26
A strong try_lock is a potentially "blocking" operation because it doesn't have an upper limit on the number of iterations. In practice an implementation would probably not fail after the first spurious LL/SC failure, but it may decide to fail after, say, 16 attempts. Or 16384 attempts.
0
u/[deleted] Feb 20 '26
[deleted] — view deleted comment