cameron314 / concurrentqueue

A fast multi-producer, multi-consumer lock-free concurrent queue for C++11
Other
9.53k stars 1.66k forks source link

Need exit mechanism for sample: Threadpool task queue #339

Closed poor1017 closed 1 year ago

poor1017 commented 1 year ago

Hi @cameron314

I read all the samples in concurrentqueue/samples.md, they are great, except:

For sample: Threadpool task queue I think we should replace wait_dequeue() with wait_dequeue_timed(). Because there is a while (true), so we need a break to exit this while, but wait_dequeue() may cause the break never be achieved.

Thank you!

cameron314 commented 1 year ago

Ah, the assumption was that one of the tasks would cause the processing code to break out of the loop. I could probably make that more clear (at the expense of a longer example).

poor1017 commented 1 year ago

If using wait_dequeue() and the queue may be empty all the time, the loop may hang all the time, at this time, how to break out of the loop?

And now, if some one destroy the queue, an undefined error occurs?

cameron314 commented 1 year ago

It's the responsibility of whoever owns the thread (and queue) to tell it to clean itself up. There's multiple ways to do this, but a common one is to set a flag and enqueue dummy data to wake up the thread, then join it.

poor1017 commented 1 year ago

Got it, thank you!