Assume a situation where a program with two threads (A and B) is running on linux.
int global = 0;
void thread_A(void) {
atomic_store(&global, 1);
udp_send_to_CX();
}
void thread_B(void) {
block_until_udp_recv_from_CX();
int local = atomic_load(&global);
assert(local == 1);
}
Thread A sends a UDP packet to a different computer CX. Upon receiving the packet, CX will send a UDP packet that will be recieved by thread B. CX will not send the UDP packet until after it has received the packet sent by thread A.
Before sending the UDP packet, thread A stores the value 1 in the global variable.
After receiving the UDP packet, thread B reads the value of the global variable.
My question is:
Is the assert in thread B guaranteed to succeed.
That is, does the store in thread A happen before the load in thread B in the sense of the C++ standard.
Note that we cannot make any assumptions regarding the machine on which thread A and thread B are running. The UDP packet sent by thread A might be sent via a different link than then one that is used to receive the UDP packet in thread B.
Threads A and B will also possibly use different sockets to send and receive the packets.
Edit: Clarification in response to some of the comments and answers below:
If there was for example a global atomic counter inside the linux kernel that was incremented with acquire-release orderings immediately before sending a packet and immediately after receiving a packet, then the answer to the question would be "Yes, the assert always succeeds". For example the send
and recv
syscalls could be implemented as follows:
int network_counter = 0;
int send(...) {
atomic_inc(&network_counter, 1, acquire_release);
...
}
int recv(...) {
...
atomic_inc(&network_counter, 1, acquire_release);
}
For any answer to be accepted, it will have to explain whether there is such a mechanism that ensures the existence of a certain timeline across all network operations or not. This question is specifically about the linux kernel.
(I do not believe that there is such a counter and if there is, then I suspect it will be link-local. This is why I specified that there might be multiple links in the system.)