4 ms·
If you can swing it (don't need to block on IO indefinitely), I'd suggest just the simple coordination model. * Some atomic bool controls if the thread shoul
by loeg 1y ago
If you can swing it (don't need to block on IO indefinitely), I'd suggest just the simple coordination model.
* Some atomic bool controls if the thread should stop or not;
* The thread doesn't make any unbounded wait syscalls;
* And the thread uses pthread_cond_wait (or equivalent C++ std wrappers) in place of sleeping while idle.
To kill the thread, set the stop flag and cond_signal the condvar. (Under the hood on Linux, this uses futex.)
- OskarS 1y agoThe tricky part is really point 2 there, that can be harder than it looks (e.g. even simple file I/O can be network drives). Async IO can really shine here, though it’s not exactly trivial designing async cancelletion either.
- deleted 1y ago[deleted]
- asveikau 1y agoRelying heavily on a check for an atomic bool is prone to race conditions. I think it's cleaner to structure the event loop as a message queue and have a queued message that indicates it's time to stop.
- PaulDavisThe1st 1y agoEvery event loop is subject to the blocked-due-to-long-running-computation issue. It bites ...
- asveikau 1y agoThe same is true if you're repeatedly polling an atomic boolean in an event loop.
- CamperBob2 1y agoHow so? It takes only a couple of machine cycles to poll a boolean. (And what other kind of boolean is there, besides atomic? It's either true or it's false, and if nothing can set it back to false once it goes true, I don't see the hazard. It's a CPU, not an FPGA.)
- kbolino 1y agoThe type is named atomic, but atomicity is not its only useful property. The atomic types also give control over the memory ordering, defaulting to sequentially consistent (seq_cst, the strongest). Without memory order guarantees enforced by memory barriers, a write to the boolean in thread A is not guaranteed to be observed by thread B. That matters both after initialization--where thread A sets the boolean to false but thread B may observe true, false, or invalid--and also after the transition--where thread B may fail to observe that the boolean has flipped from false to true. [edit: I'm not sure the above reasoning actually matters; as stated already by parent, "It's a CPU, not an FPGA"; modern multicore shared-memory CPUs have coherent caches]
- CamperBob2 1y agoWell, it'll be observed the next time through the loop. If that matters, then it's true that this technique isn't desirable.
- manwe150 1y agoWithout atomic, the compiler won’t bother with there being a next time and just infinite loop (in the old days, you’d mark it volatile instead)
- CamperBob2 1y agoTrue enough, but volatile still works, of course.
- adwn 1y ago> Without memory order guarantees enforced by memory barriers, a write to the boolean in thread A is not guaranteed to be observed by thread B. No, that's not correct. Memory ordering doesn't influence how fast a write is propagated to other cores, that's what cache coherency is for. Memory ordering of an access only matters in relation to accesses on other memory locations. There's a great introduction to this topic by Mara Bos: https://marabos.nl/atomics/memory-ordering.html https://marabos.nl/atomics/memory-ordering.html
- deleted 1y ago[deleted]
- a-dub 1y agodisagree. i think then it's too tempting down the line for someone to add a message with blocking processing. a simple clear loop that looks for a requested stop flag with a confirmed stop flag works pretty well. this can be built into a synchronous "stop" function for the caller that sets the flag and then does a timed wait on the confirmation (using condition variables and pthread_cond_timedwait or waitforxxxobject if you're on windows).
- asveikau 1y agoMaking your check less stable doesn't prevent this. The examples in this article IIRC were something like this. while (check_flag()) { do_stuff(); sleep_like_a_moron_instead_of_proper_blocking_mechanism(1second); } You're still going to be arbitrarily delayed if do_stuff() (or one one of its callees, maybe deep inside the stack) delays, or the sleep call. If you can't accept this, maybe don't play with threads, they are dangerous.
- a-dub 1y agothat's the point. use nonblocking io and an event polling mechanism with a timeout to keep an eye on an exit flag- that's all you need to handle clean shutdowns. i think on windows you can wait on both the sockets/file descriptors and condition variables with the same waitforxxxobject blocking mechanism. on linux you can do libevent, epoll, select or pthread_cond_timedwait. all of these have "release on event or after timeout" semantics. you can use eventfd to combine them. i would not ever recommend relying on signals and writing custom cleanup handlers for them (!). unless they're blocked waiting for an external event, most system calls tend to return in a reasonable amount of time. handle the external event blocking scenario (stuff that select waits for) and you're basically there. moreover, if you're looking to exit cleanly, you probably don't want to take your chances interrupting syscalls with signals (!) anyway. > If you can't accept this, maybe don't play with threads, they are dangerous. too late. when i first started playing with threads, linux didn't really support them.
- asveikau 1y ago
- toast0 1y agoQueuing a stop means you have to process the queue before stopping. Which certainly is stopping cleanly, but if you wanted to stop the thread because its queue was too long and the work requests were stale, it doesn't help much. You could maybe allow a queue skipping feature to be used for stop messages... But if it's only for stop messages, set an atomic bool stop, then send a stop message. If the thread just misses the stop bool and waits for messages, you'll get the stop message; if the queue is large, you'll get the stop bool. ps, hi
- loeg 1y ago> Relying heavily on a check for an atomic bool is prone to race conditions. It is not, actually. This extremely simple protocol is race-free.
- krackers 1y agoCalling pthread_cond_signal without acquiring mutex can lead to lost wakeup. And of course you can't really acquire a mutex in an async signal safe function like interrupt handler. Without the signaling thread acquiring a mutex, you might end up signaling after T2 has checked the boolean, but before it has called cond_wait. But this can be solved by processing the async signal in a deferred manner from some other watcher thread
- exDM69 1y ago> To kill the thread, set the stop flag and cond_signal the condvar This is a race condition. When you "spin" on a condition variable, the stop flag you check must be guarded by the same mutex you give to cond_wait. See this article for a thorough explanation: https://zeux.io/2024/03/23/condvars-atomic/ https://zeux.io/2024/03/23/condvars-atomic/