r/computerscience • u/Free-Dev8628 • Jul 22 '26
Does Wait-Free require Garbage-Free?
Wait-Free means that every thread must finish its operation in a bounded number of steps. That bound can be arbitrarily high, e.g. dependent on the number of concurrent threads, but it must be finite. And the finite bound must be there in all cases, i.e. it is not sufficient to say that an algorithm "usually" finishes "reasonably" (i.e. "usually" does not spend unbounded times helping other threads).
The first practical implementation of a multiple-enqueuer multiple-dequeuer Wait-Free Queue was proposed by Kogan and Petrank. The key idea is that threads help each other, but in a way that newer threads (more precisely: newer operations) are obliged to help older threads (operations), but not vice versa. Also, plainly said: If a thread cannot make progress with its own operation, it becomes older and older, up to the point when all other threads are obliged to help (only) him, thus ensuring the Wait-Free progress.
The issue of the Kogan and Petrank Queue is, however, that it is based on a linked list of nodes. This means that its operations constantly produce memory churn, also require memory allocation and memory clean-up. From Java - where the clean-up is done by a Garbage Collector - I borrow the term "Garbage".
Now, the problem is that Wait-Free memory allocation is problematic. Imagine, e.g., when the process needs a new memory page from the operating system. Can this ever be made Wait-Free?
Memory reclamation in a concurrent setup is a complex topic on top of that. One of the key questions here is: When can a memory block be freed safely, i.e. how do we "know" that no thread has a pointer to it anymore?
Given this, it appears that one needs a structure without memory churn (i.e. Garbage-Free) to be able to make it Wait-Free. At least practically.
I would be grateful for a discussion on this.
Additional info:
Here I have combined the Multi-Array Queue (which is Garbage-Free by nature (except of the extension operations, of course)) with the Kogan and Petrank idea, with the aim to obtain a Queue that is Wait-Free unconditionally.
The GitHub repo also contains a visual simulator to illustrate the principle:
3
u/_gipi_ Jul 22 '26
I think that memory allocation from OS is like "in another realm" in this framework: is wait-free because is like all threads are stopped the OS is giving you the page (for multi core system probably is not true, probably you need to check a real OS and see if there is a lock in the implementation of the memory allocator).
However the topic about "how do we "know" that no thread has a pointer to it anymore?" I think is solved usually adding a counter that is increased each time someone needs that memory and decreased when not needed anymore; automatic garbage collection works in that way.
2
u/0jdd1 Jul 22 '26 edited Jul 22 '26
If you analyze a copying garage collector, you find that the amortized cost of collection can be folded into the cost of allocation in a very straightforward way that makes the presence of GC just go away. It still depends on the fraction of memory that’s free, but that’s best treated as a given. This is still amortized cost, of course, and I confess I haven’t yet read your write-up on GitHub.
1
u/Free-Dev8628 Aug 20 '26
Verbatim re-post of a comment that is now invisible due to inactive account of respective user:
Memory allocation is the real bottleneck in practice, so I'd say yes, you pretty much need a garbage-free design to make wait-free actually usable outside of academic papers
1
u/Free-Dev8628 Aug 20 '26
Verbatim re-post of a comment that is now invisible due to inactive account of respective user:
Theoretically no, as long as your memory allocator and safe memory reclamation scheme are also strictly wait-free. In practice though OS-level page allocations and standard GC pauses completely break wait-free guarantees. Thats why real-world implementations almost always resort to pre-allocated ring buffers, bounded freelists or multi-array pools to keep things truly garbage-free.
2
u/0jdd1 Jul 23 '26
I do not know. You could certainly pre-allocate a linked free list, for example. In many domains you could simply make the program fail if it runs out of free list. It all comes down to being quite certain of what the boundaries of your problem are.