简体   繁体   中英

Multi producer single consumer queue without dedicated consumer thread

I'm writing an async application which submits elements to a work queue for processing. The requirements are:

  • There is no background thread listening on the queue. Any thread that submits an element to the queue may be responsible for consuming the queue.
  • There can be multiple concurrent producers, but only one thread can be the active consumer.
  • If thread A submits an element but thread B is actively consuming the queue, thread A should return immediately. No waiting allowed.

Here is an example of what I'm trying to do:

final Queue<T> queue = new ConcurrentLinkedQueue<>();
final AtomicBoolean processing = new AtomicBoolean();

void offer(T t) {
    queue.offer(t);

    for (;;) {
        if (processing.compareAndSet(false, true)) {
            try {
                for (;;) {
                    T t = queue.poll();
                    if (t == null) {
                        break;
                    }
                    // process t
                }
            } finally {
                processing.set(false);
            }

            // see if another thread submitted an element but didn't process it
            // if not, we can break
            if (queue.isEmpty()) {
                break;
            }
        } else {
            // losers should exit
            break;
        }
    }
}

I think my code works, but this problem sounds fairly standard so I would like to know if there is any textbook solution or at least a common name. So far I only find algorithms that require a worker thread to be listening on the queue.

Your code is correct, but problem is not seen as standard: usually, having background thread is not so expensive, as:

  1. Having no finite time garantee for producers. It is possible for some producer to work for a long time in consumer mode.

  2. Complicate producer's code.

In any case, your question's title ( MPSC without dedicated consumer thread ) describes the problem well. For resolve it you can combine trylock/unlock approach with any MPSC implementation without notification mechanism. The only additional requirement to MPSC is concurrent queue.isEmpty() method, but usually it is not a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM