16

I'm currently writing a simple webserver in C for a course I'm doing. One requirement is for us to implement a thread pool to handle connections using pthreads.

I know how I would go about doing this roughly(calling accept in a main thread and passing the file descriptor to a freee thread), however my friend suggested an alternate method than the one I had in mind: creating all my threads up front, and getting them all to loop forever on a call to accept. The idea being that accept will block all the idle threads and when a connection comes in, only giving the file descriptor to one of them. Then when a given thread is done with a connection it loops back around and blocks on a call to accept again. Using the call to accept() as a semaphore essentially. This would simplify the implementation quite a bit he figures, as you wouldn't need to keep track of which threads are busy and which are ready for a connection. It would also be lower latency in theory, as the thread can immediately start executing (without the need to create it).

My question is, is this safe? I'm planning to implement it and try it out, but I'm not ready yet and I'm quite curious to know the answer. I've searched on google and here on stackoverflow, but couldn't find anyone doing it this way. Is accept thread safe? I assume there will be more overhead with this approach as you are running all your threads all the time, are the two approaches simply a simple memory/latency tradeoff?

Edit: I'm unsure if this should be community wiki, apologies if it should be, I can't find the button :P

10
  • Be a bit careful here, the POSIX definition of "thread-safe" means that you can call the function concurrently from different threads. It doesn't imply that you necessarily can call the function concurrently mutating the same data with predictable results or even defined behavior. 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. Commented Feb 26, 2011 at 1:13
  • 5
    @Steve: While that's a valid warning for interfaces to which you pass pointers, it doesn't apply to file descriptors/sockets. OP's usage is perfectly safe. Commented Feb 26, 2011 at 2:33
  • 1
    @R..: I thought it probably was safe, but where does POSIX explain/define that? Is there a specific term for it beyond "thread-safe"? Commented Feb 26, 2011 at 16:26
  • 1
    @EJP: you'd think, wouldn't you? But for example opengroup doesn't define "atomic" in the place it defines "thread-safe": pubs.opengroup.org/onlinepubs/9699919799/basedefs/… . I don't have access to the IEEE publication of the POSIX standard to check properly. Commented Jul 23, 2017 at 0:43
  • 1
    @Rick: System calls should always be thread-safe. The kernel has to have that level of safety to operate correctly. This is because all processes share the same kernel, and when inside the kernel, the execution of the system call from two separate processes look like two threads of execution in the kernel. POSIX does not define system calls, only the behaviors of the APIs. If an API gets implemented as a system call in a particular implementation, then it needs to be made thread-safe.
    – jxh
    Commented Aug 27, 2020 at 15:56

4 Answers 4

19

Yes. This is a common way to design multithreaded servers and accepted design practice.

You can also fork several times and have the child processes call accept, this will allow you to do multithreading without needing a threads library. Older servers do this.

8
  • Hey @Dietrich, could you give some references about that? I couldn't find any related authoritative materials . Check my question here Is there any authoritative place that says multi processes or threads accepting a listening socket is an atomic operation? . Thank you
    – Rick
    Commented Aug 23, 2020 at 2:30
  • 1
    @Rick - it’s a system call. It’s not a library function. There is no reasonable way for accept() to be unsafe. If it were unsafe, the kernel itself would be unsafe. You can fund a list of MT safe functions if you like. I am not going to do that, because I already know it is on that list. This is the way programs have been splitting work since before pthreads existed. Commented Aug 23, 2020 at 3:31
  • 1
    @Rick - see pubs.opengroup.org/onlinepubs/9699919799/functions/… - accept is listed as async signal safe Commented Aug 23, 2020 at 3:33
  • In some ways I get the point now. thread-safe would be the property that meets the behaviour. I was thinking thread-safe has to be threads within same process. But now I think there aren't much differences between threads within same process or threads between different processes. In some ways they are the same. So the concept of thread-safety can be applied to both scenarios.
    – Rick
    Commented Aug 23, 2020 at 15:09
  • And I think atomic and thread safe are 2 different but related terms. Thread safe would be the appropriate term to meet this scenario, not atomic. Difference between Atomic Operation and Thread Safety?.
    – Rick
    Commented Aug 23, 2020 at 15:11
3

Since this has been bountied, asking for references:

Yes, accept() is thread-safe, as POSIX defines that term.

The relevant reference would be section 2.9.1 of POSIX.1, the current version of which says:

All functions defined by this volume of POSIX.1-2017 shall be thread-safe, except that the following functions need not be thread-safe.

[a list that does not include accept()]

For completeness, POSIX does define accept(): https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html, so as a POSIX function that does not appear on the list of exceptions, POSIX specifies that it is thread-safe.

2
  • In some ways I get the point now. thread-safe would be the property that meets the behaviour. I was thinking thread-safe has to be threads within same process. But now I think there aren't much differences between threads within same process or threads between different processes. In some ways they are the same. So the concept of thread-safety can be applied to both scenarios.
    – Rick
    Commented Aug 23, 2020 at 14:38
  • @Rick, threads within the same process share the state of the process. This makes thread safety with respect to threads belonging to the same process a strictly stronger property than thread safety with respect to threads of different processes. In most regards, process isolation makes cross-process thread safety a non-issue. Commented Aug 23, 2020 at 19:23
2
+300

In comments, @Rick (the bounty offerer) says:

In some ways I get the point now. thread-safe would be the property that meets the behaviour. I was thinking thread-safe has to be threads within same process. But now I think there aren't much differences between threads within same process or threads between different processes. In some ways they are the same. So the concept of thread-safety can be applied to both scenarios.

The POSIX definitions regarding thread-safety do indeed refer to threads in the same process (see: §2.9).

If you are asking about what happens after fork(), and if it is safe for the parent and child to concurrently call into accept(), we note first that POSIX defines a system resource known as a connection indication queue. Then we note that the child of fork() gets a copy of the parent's descriptors, and therefore the child and parent will access the same connection indication queue (just as a duplicate file descriptor to a text file would be accessing the same file).

The definition of what accept() does for each process (child and parent) is the same at that point.

6
  • Ah. I know that connection indication queue. It's what backlog in int listen(int sockfd, int backlog) refers to.
    – Rick
    Commented Aug 25, 2020 at 8:51
  • Also, thank you for clearing some of my doubts about parent and child concurrently call into accept(). I just read the §2.9 section. And yes, thread-safety is defined as threads in the same process. And my idea about "thread-safety between different processes" is heading the wrong direction. The problem is more likely related to multiple threads or processes concurrently access the connection indication queue system-wide resource you mention.
    – Rick
    Commented Aug 25, 2020 at 8:59
  • @Rick: Right, listen() will associate a connection indication queue to a socket. So, if a socket gets duplicated (either with dup() or copied to a child via fork()) the sockets refer to the same connection indication queue.
    – jxh
    Commented Aug 25, 2020 at 17:58
  • @Rick: The API definitions give the behavior of the call from the process. Each process is supposed to see the same behavior as specified. So accept() from both a child and parent process should just work. The OP was asking about from multiple threads in the same process, and that is also defined to work, since accept() is thread-safe.
    – jxh
    Commented Aug 25, 2020 at 18:15
  • Sorry to be pedantic. You said "The API definitions give the behavior of the call from the process. Each process is supposed to see the same behavior as specified." I don't understand what you are exactly refer to here, because when I searched man 2 accept with keyword process, I don't find anything related or useful.
    – Rick
    Commented Aug 29, 2020 at 1:07
1

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 acceptance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.