Standard way of handling both signals and IO streams


As a learning exercise i am trying to reproduce a C++ intervew i had a while ago, in rust.

The idea is to develop a TCP server that can accept 4 TCP connections and send them a unique identifier every second, this identifier computation being "performance sensitive" (IDs must be unique across clients). If ctrl-C is pressed on the server, it must send "goodbye" to each client and gracefully close connections.

My main interrogation is about the handling of ctrl-c :
I guess i could simply have a shared boolean and provide it to the 4 threads so they periodically check its value, but is there a more "standard" way of doing this in rust ? Reading about channels and selects for instance made me think there might be a better design but i can't figure out what it would look like for now. Since it is mainly to learn rust i am not using framework such as tokio for instance but i use the ctrlc crate.

Do you have any suggestion on the overrall design ?

Thanks !

What did you do in C++?

I would probably use a static with AtomicBool, but passing &Cell<bool> is also fine.

That's not going to compile across threads. It's not thread safe.

1 Like

Oh no, may bad. This is what happens when you suggest without checking first :wink: