Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Concurrent read / write of serial port

Posted on 2006-07-14
Medium Priority
Last Modified: 2010-04-21
I have a serial port access class which wraps the termios API. This serial port in some situations is used in a multithreaded environment. In this case it will be shared by 2 threads but I can guarantee that 1 will perform output only, and the other will perform input only. Does this require a mutex to access the object?

I can't find any documentation on the thread-safety of read(2) and write(2), or termios(3). I guess it really depends on the implementation, but given that read, write and the termios API are POSIX-compliant, does anyone know if POSIX specifies the thread-safety of these functions?

Question by:boycy
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 17106913
> does anyone know if POSIX specifies the thread-safety of these functions?

No the POSIX does not say anything about thread safety of those functions. This means that thread safety is not guranteed and one should assume that they are not thread safe.

> Does this require a mutex to access the object?
At the hardware level, Serial ports have seperate pins for data transmission and reception.
So, you may not need to protect read from writes.

Manish Regmi

Author Comment

ID: 17106951
Granted the hardware level is fine for concurrent read/write - after all, it is full duplex. I would *assume* that providing an API which removes this full duplex ability would be a really silly idea; however assumptions are not good things. If there is any doubt at all as to whether these functions are thread-safe then I cannot use them. I'm having real trouble finding any information on this subject.

Accepted Solution

manish_regmi earned 500 total points
ID: 17106982
A discussion on red hat forum also suggests it is safe.
So under Linux it may be safe but may not be for other OS.

Also since POSIX does not confirm thread safety. You can protect them with mutex for safety.

Manish Regmi

Author Comment

ID: 17107000
Manish, thanks :)

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question