First of all, get rid of your secret macro language so that other programmers who don't know your what your secret names mean can read the code. This means replace the mysterious, unreadable SERVER_LOCK
with the super-readable pthread_mutex_lock(&mtx)
and so on.
Second, grabbing a mutex and holding on to it while the program does I/O or unrelated string comparisons etc is very bad for performance. You do this all over, so it's a massive performance killer.
What you should do is this:
if (strcmp(in_buf, "start") == 0) { printf("=> Start receiving packets\n"); pthread_mutex_lock(&mtx) rx_is_allowed = 1; pthread_mutex_unlock(&mtx); } else if (strcmp(in_buf, "stop") == 0) { printf("=> Stop receiving packets\n"); pthread_mutex_lock(&mtx) rx_is_allowed = 0; pthread_mutex_unlock(&mtx); } else { printf("Invalid input buffer\n"); }
That is, hold on to the mutex as short a time as possible and only for the purpose of protecting variables shared between threads. In this case only when you modify the rx_is_allowed
variable (or any such variable shared between threads). Fix this and I'm pretty certain all the mentioned problems will disappear.