{
    "mode": "man",
    "parameter": "futex",
    "section": "7",
    "url": "https://www.chedong.com/phpMan.php/man/futex/7/json",
    "generated": "2026-10-07T14:53:26Z",
    "synopsis": "",
    "sections": {
        "NAME": {
            "content": "futex - fast user-space locking\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "",
            "subsections": [
                {
                    "name": "#include <linux/futex.h>",
                    "content": ""
                }
            ]
        },
        "DESCRIPTION": {
            "content": "The  Linux  kernel  provides futexes (\"Fast user-space mutexes\") as a building block for fast\nuser-space locking and semaphores.  Futexes are very  basic  and  lend  themselves  well  for\nbuilding  higher-level  locking abstractions such as mutexes, condition variables, read-write\nlocks, barriers, and semaphores.\n\nMost programmers will in fact not be using futexes directly but will instead rely  on  system\nlibraries built on them, such as the Native POSIX Thread Library (NPTL) (see pthreads(7)).\n\nA  futex is identified by a piece of memory which can be shared between processes or threads.\nIn these different processes, the futex need not have identical addresses.  In its bare form,\na futex has semaphore semantics; it is a counter that  can  be  incremented  and  decremented\natomically; processes can wait for the value to become positive.\n\nFutex  operation  occurs entirely in user space for the noncontended case.  The kernel is in‐\nvolved only to arbitrate the contended case.  As any sane  design  will  strive  for  noncon‐\ntention, futexes are also optimized for this situation.\n\nIn its bare form, a futex is an aligned integer which is touched only by atomic assembler in‐\nstructions.   This integer is four bytes long on all platforms.  Processes can share this in‐\nteger using mmap(2), via shared memory segments, or because they share memory space, in which\ncase the application is commonly called multithreaded.\n",
            "subsections": [
                {
                    "name": "Semantics",
                    "content": "Any futex operation starts in user space, but it may be necessary  to  communicate  with  the\nkernel using the futex(2) system call.\n\nTo  \"up\"  a  futex, execute the proper assembler instructions that will cause the host CPU to\natomically increment the integer.  Afterward, check if it has in fact changed from 0 to 1, in\nwhich case there were no waiters and the operation is done.  This is  the  noncontended  case\nwhich is fast and should be common.\n\nIn the contended case, the atomic increment changed the counter from -1  (or some other nega‐\ntive number).  If this is detected, there are waiters.  User space should now set the counter\nto 1 and instruct the kernel to wake up any waiters using the FUTEXWAKE operation.\n\nWaiting on a futex, to \"down\" it, is the reverse operation.  Atomically decrement the counter\nand  check  if  it changed to 0, in which case the operation is done and the futex was uncon‐\ntended.  In all other circumstances, the process should set the counter  to  -1  and  request\nthat  the kernel wait for another process to up the futex.  This is done using the FUTEXWAIT\noperation.\n\nThe futex(2) system call can optionally be passed a timeout specifying how  long  the  kernel\nshould wait for the futex to be upped.  In this case, semantics are more complex and the pro‐\ngrammer  is  referred  to  futex(2)  for more details.  The same holds for asynchronous futex\nwaiting.\n"
                }
            ]
        },
        "VERSIONS": {
            "content": "Initial futex support was merged in Linux 2.5.7 but with different semantics from  those  de‐\nscribed above.  Current semantics are available from Linux 2.5.40 onward.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "To reiterate, bare futexes are not intended as an easy-to-use abstraction for end users.  Im‐\nplementors  are  expected  to  be assembly literate and to have read the sources of the futex\nuser-space library referenced below.\n\nThis man page illustrates the most common use of the futex(2) primitives; it is by  no  means\nthe only one.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "clone(2), futex(2), getrobustlist(2), setrobustlist(2), settidaddress(2), pthreads(7)\n\nFuss,  Futexes and Furwocks: Fast Userlevel Locking in Linux (proceedings of the Ottawa Linux\nSymposium 2002), futex example library, futex-*.tar.bz2  https://mirrors.kernel.org/pub/linux\n/kernel/people/rusty/.\n\nLinux man-pages 6.7                          2023-10-31                                     futex(7)",
            "subsections": []
        }
    },
    "summary": "futex - fast user-space locking",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "clone",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/clone/2/json"
        },
        {
            "name": "getrobustlist",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/getrobustlist/2/json"
        },
        {
            "name": "setrobustlist",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/setrobustlist/2/json"
        },
        {
            "name": "settidaddress",
            "section": "2",
            "url": "https://www.chedong.com/phpMan.php/man/settidaddress/2/json"
        },
        {
            "name": "pthreads",
            "section": "7",
            "url": "https://www.chedong.com/phpMan.php/man/pthreads/7/json"
        }
    ]
}