google/nsync logo

google/nsync

Free

nsync is a C library that exports various synchronization primitives, such as mutexes

FreeFree tier
Type
Open Source
Company
Google

About google/nsync

nsync is a C library developed by Google that provides efficient synchronization primitives for multithreaded programming. It offers mutexes (reader-writer locks), condition variables, run-once initialization, waitable counters (useful for barriers), waitable bits (for cancellation or other conditions), and conditional critical sections. The library is designed to be portable, working on Unix-like systems and Windows, and can be compiled with a C90 compiler assuming atomic operations are available. nsync locks and condition variables occupy only two words each, making them memory-efficient. Conditional critical sections simplify usage by eliminating the need for explicit while loops and wake signals, though they may be slower under many false conditions. The library is not an official Google product.

Key Features

Reader-writer locks as efficient as mutexes
Condition variables
Run-once initialization
Waitable counter (useful for barriers)
Waitable bit (useful for cancellation or other conditions)
Conditional critical sections (easier to use than condition variables)
Portable: works on Unix-like systems and Windows
Small memory footprint (locks and condition variables occupy two words each)
Compilable with C90 or C++ compilers
Supports cancellation of waits via objects

Pros & Cons

Pros
  • Reader-writer locks are as efficient as mutexes but allow concurrent reads
  • Conditional critical sections eliminate the need for explicit while loops and wake signals
  • Very small memory footprint (two words per lock/condition variable)
  • Portable across Unix-like and Windows platforms
  • Can be compiled with C90, C11, C++11, or C++14
  • Supports cancellation of waits, unlike pthread thread cancellation
Cons
  • Conditional critical sections may be slower if many threads wait on many distinct false conditions
  • Not as widely known as pthreads; may require learning
  • No class-based C++ interface; uses C-style API

Best For

Multithreaded C/C++ applications needing synchronizationSystems programming requiring portable locks and barriersImplementing cancellation or conditional waits in concurrent code

FAQ

What synchronization primitives does nsync provide?
nsync provides mutexes (reader-writer locks), condition variables, run-once initialization, waitable counters (useful for barriers), waitable bits (for cancellation/conditions), and conditional critical sections.
Is nsync portable?
Yes, nsync works on Unix-like systems and Windows and can be ported to other platforms. It can be compiled with a C90 compiler, assuming atomic operations are available.
How do conditional critical sections differ from condition variables?
Conditional critical sections are usually easier to use: they don't require surrounding the wait in a while loop, and conditions are checked automatically without explicit signaling. However, they may be slower if many distinct false conditions are common.