I have implemented a multithreaded application using pthread. In this application there are two threads:
- The first polls a tap port in order to read the available data and write it to a serial port to which a radio is connected.
- The second vice versa polls the serial port and then writes the data to the tap port.
To avoid data race problems before accessing a port (serial or tap) I use a pthread_mutex_t. On https://man7.org/linux/man-pages/man7/pthreads.7.html I read that read() and write() are cancellation points, that is, they are points where a thread can potentially be canceled.
Pseudo-Code Example:
pthread_mutex_t serial_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t tap_mutex = PTHREAD_MUTEX_INITIALIZER;
atomic_bool continue = true;
//T1
void* run(void* vargp)
{
int poll_timeout = 1000;
while (continue)
{
int poll_result = poll(&tap_fd, 1, poll_timeout);
if (poll_result != -1 && poll_result != 0)
{
if (tap_fd.revents & POLLIN)
{
pthread_mutex_lock(&tap_mutex);
int tap_len = read(tap, tap_buffer, sizeof(tap_buffer));
pthread_mutex_unlock(&tap_mutex);
if(tap_len >= MIN_SIZE)
{
/*
In reality, the contents of the tap buffer are preprocessed and the
contents of another buffer are written to the serial
*/
pthread_mutex_lock(&serial_mutex);
int r = write(serial, tap_buffer, tap_len);
pthread_mutex_unlock(&serial_mutex);
}
}
}
}
//T2 is completely analogous to the previous one
Since read and write are both performed in a critical section, would the mutex be automatically released if the thread were to be canceled? And if not, how can I guarantee the release of the relative mutex?