Sending 2 Independent UDP packets on same socket from different tasks. Is this thread safe?

It is only allowed to use a socket for reading in one task, and for writing in a different task.

It is not safe to share a socket when both tasks are writing to it or when both tasks are reading from it.

Two reasons:

  1. It has to do with the use of event-groups.
    Both tasks will wait for an eSOCKET_SEND event, and both will set xClearOnExit.

  2. The data will be copied to a circular buffer, the socket’s TX stream buffer. Circular buffers assume that there is only one writer and one reader. If not, the results are unpredictable.

Can’t you make one single task the owner of the UDP socket? And if you want to send a UDP packet, send it to the owning task who will forward it?

2 Likes