Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could probably implement a limited-size queue with least difficulty... I was thinking about it lately and came up with this design, but you can probably find many other interesting ideas: (WARNING: it might have some issues!)</p> <ul> <li>the queue is an array of pointers to items</li> <li>you have to manage 2 pointers (head, tail) which work over the queue in the same way as a circular buffer</li> <li>if <code>head</code> == <code>tail</code>, there are no items</li> <li>if you want to <code>enqueue(ptr)</code>, Interlocked-Swap <code>tail</code> with NULL (<code>prev_tail</code> is the swapped value) <ul> <li>if <code>prev_tail == NULL</code>, try again</li> <li>if <code>prev_tail + 1</code> (with wraparound) == <code>head</code>, your queue is full</li> <li>otherwise put your <code>ptr</code> in <code>*prev_tail</code> and assign <code>prev_tail+1</code> to <code>tail</code> (watch out for buffer wrap-around)</li> </ul></li> <li>for <code>dequeue()</code> make a copy tmp_head=head and check <code>tmp_head == tail</code> <ul> <li>if it's true, return because the queue is empty</li> <li>if it's false <ul> <li>save <code>*tmp_head</code> as <code>ptr</code></li> <li>do a CAS: compare <code>head</code> with <code>tmp_head</code> swap <code>head</code> with <code>head+1</code></li> <li>if CAS failed -- start the whole function over</li> <li>if it succeeded -- return <code>ptr</code></li> </ul></li> </ul></li> </ul> <p>You can wait on both head and tail CAS operations, but if the queue is not contended, you should succeed the first time, without unnecessary locks.</p> <p>Unlimited-size queues are "a bit" harder ;) But you should be able to create a big-enough queue for most needs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload