Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blocking read() and write() on multiple threads in Linux

I have a Linux process that needs to read from, say, the keyboard and mouse. It needs to do this asynchronously while also doing some main process.

The implementation I've come up with is to have the main process in one thread, and have two separate threads always read()ing from the keyboard and mouse. If Linux read() is blocking, will the keyboard and mouse threads become blocked while the main thread continues executing (what I want), or will the entire process become blocked?

like image 537
tomKPZ Avatar asked Aug 23 '26 05:08

tomKPZ


1 Answers

All threads in a process will not block as a result of one or more threads in the process becoming blocked. Each thread will be scheduled if it is able to run, within the rules of the scheduler in effect.

So your design is quite valid. Just use proper synchronization techniques if the data you read has to be consumed by another thread. Semaphores and message queues are handy for that purpose.

like image 128
Amardeep AC9MF Avatar answered Aug 26 '26 01:08

Amardeep AC9MF



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!