Applications link against the libc
implementation on a system in order to call accept()
and other socket-related functions (#include <sys/socket.h>
). You want to read its documentation.
The most common implementation of libc
on Linux comes from GNU (or maybe bionic
from Google on Android), it is called glibc
and it is very likely what you are (will be) using. As stated in accept
documentation for glibc
:
Function: int accept (int socket, struct sockaddr *addr, socklen_t *length_ptr)
Preliminary: | MT-Safe | AS-Safe | AC-Safe fd | See POSIX Safety Concepts.
As explained in POSIX Safety concepts, the Preliminary section enumerates properties which:
are assessed according to the criteria set forth in the POSIX standard for such safety contexts as Thread-, Async-Signal- and Async-Cancel- -Safety.
And an explanation of such concepts follow (also, check "Thread Safety" on wikipedia for different approaches to achieve thread safety). accept
is declared as MT-Safe, according to doc:
MT-Safe or Thread-Safe functions are safe to call in the presence of other threads. MT, in MT-Safe, stands for Multi Thread.
Being MT-Safe does not imply a function is atomic, nor that it uses any of the memory synchronization mechanisms POSIX exposes to users. It is even possible that calling MT-Safe functions in sequence does not yield an MT-Safe combination. For example, having a thread call two MT-Safe functions one right after the other does not guarantee behavior equivalent to atomic execution of a combination of both functions, since concurrent calls in other threads may interfere in a destructive way.
Whole-program optimizations that could inline functions across library interfaces may expose unsafe reordering, and so performing inlining across the GNU C Library interface is not recommended. The documented MT-Safety status is not guaranteed under whole-program optimization. However, functions defined in user-visible headers are designed to be safe for inlining.
The fact that glibc
's accept
implementation just redirect to the kernel system call, makes this description a useful one also for other libc
implementations on Linux systems (which likely just perform a redirection to the system call too).
On the other hand, a more general approach is to check the man-pages project
on your system if available (the closest thing to an official documentation on most systems), which:
[...] documents the Linux kernel and C library interfaces that are employed by user-space programs. With respect to the C library, the primary focus is the GNU C library (glibc), although, where known, documentation of variations on other C libraries available for Linux is also included.
By typing man 2 accept
on the command line:
[...]
CONFORMING TO
accept(): POSIX.1-2001, POSIX.1-2008, SVr4, 4.4BSD (accept() first
appeared in 4.2BSD).
We see that POSIX.1-2008
is a viable reference (check this for a description of relevant standards for Linux systems). As already said in other answers, POSIX.1
standard specifies accept
function as (POSIX-
)thread safe (as defined in Base Definitions, section 3.399 Thread Safe) by not listing it on System Interfaces, section 2.9.1 Thread Safety.
Finally, as glibc
just delegates on kernel's accept()
, the most reputable source is the kernel source code (of course). This answer goes through kernel code path when accept()
ing: take a look and convince yourself that shared resources are protected by spin-locks, in particular the socket state and the queue of connections waiting for application accept
ance.
strcpy
is thread-safe, but it won't take any kind of lock. In POSIX, "not thread safe" means very unsafe. By the POSIX definition,accept()
is thread-safe (ie not on the list here: pubs.opengroup.org/onlinepubs/009695399/functions/…), but you need more than that.