"lock-free thread-safe queue - need advice" Code Answer


"lock-free"does not mean that threads won't block each other. it means that they block each other through very efficient but also very tricky mechanisms. only needed for very high performance scenarios and even the experts get it wrong (a lot).

best advice: forget "lock-free"and just use a "thread-safe" queue.

i would recommend the "blocking queue" from this page.

and it's a matter of choice to include the threadroutine (the consumer) in the class itself.

to the second part of your question, it depends on what "some synchronization event" exactly is. if you are going to use a method call, then let that start a one-shot thread. if you want to wait on a semaphore than don't use monitor and pulse. they are not reliable here. use an autoresetevent/manualresetevent.
how to surface that depends on how you want to use it.

your basic ingredients should look like this:

class logger
    private autoresetevent _waitevent = new autoresetevent(false);
    private object _locker = new object();
    private bool _isrunning = true;    

    public void log(string msg)
       lock(_locker) { _queue.enqueue(msg); }

    public void flushqueue()

    private void workerproc(object state)
        while (_isrunning)
            // process queue, 
            // ***
                string s = null;
                   if (_queue.isempty) 
                   s = _queue.dequeu();
                if (s != null)
                  // process s

part of the discussion seems to be what to do when processing the queue (marked ***). you can lock the queue and process all items, during which adding of new entries will be blocked (longer), or lock and retrieve entries one by one and only lock (very) shortly each time. i've adde that last scenario.

a summary: you don't want a lock-free solution but a block-free one. block-free doesn't exist, you will have to settle for something that blocks as little as possible. the last iteration of mys sample (incomplete) show how to only lock around the enqueue and dequeue calls. i think that will be fast enough.

By Derek E on February 13 2022

Answers related to “lock-free thread-safe queue - need advice”

Only authorized users can answer the Search term. Please sign in first, or register a free account.