Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial communication C/C++ Linux thread safe?

My question is quite simple. Is reading and writing from and to a serial port under Linux thread-safe? Can I read and write at the same time from different threads? Is it even possible to do 2 writes simultaneously? I'm not planning on doing so but this might be interesting for others. I just have one thread that reads and another one that writes.

There is little to find about this topic.

More on detail—I am using write() and read() on a file descriptor that I obtained by open(); and I am doing so simultaneously.

Thanks all!

Roel

like image 940
Silver Avatar asked Oct 21 '22 13:10

Silver


1 Answers

There are two aspects to this:

  1. What the C implementation does.
  2. What the kernel does.

Concerning the kernel, I'm pretty sure that it will either support this or raise an according error, otherwise this would be too easy to exploit. The C implementation of read() is just a syscall wrapper (See what happens after read is called for a Linux socket), so this doesn't change anything. However, I still don't see any guarantees documented there, so this is not reliable.

If you really want two threads, I'd suggest that you stay with stdio functions (fopen/fread/fwrite/fclose), because here you can leverage the fact that the glibc synchronizes these calls with a mutex internally.

However, if you are doing a blocking read in one thread, the other thread could be blocked waiting to write something. This could be a deadlock. A solution for that is to use select() to detect when there is some data ready to be read or buffer space to be written. This is done in a single thread though, but while the initial code is a bit larger, in the end this approach is easier and cleaner, even more so if multiple streams are involved.

like image 162
Ulrich Eckhardt Avatar answered Nov 15 '22 04:11

Ulrich Eckhardt