FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
C++Tasks & synchronization

Check whether a future is ready without blocking

C++: A zero-duration wait reports timeout until the producer sets a value.

C++✓ Tested
C++Tasks & synchronization

Convert a duration to another unit explicitly

C++: duration_cast converts units and truncates fractional integer units.

C++✓ Tested
C++Tasks & synchronization

Deliver a result through a promise and future

C++: A promise sets a shared state and its future retrieves the value.

C++✓ Tested
C++Tasks & synchronization

Increment a shared counter safely

C++: Atomic increments prevent lost updates when two threads change the same counter.

C++✓ Tested
C++Tasks & synchronization

Observe an exception from an asynchronous task

C++: Calling get rethrows an exception captured by the future.

C++✓ Tested
C++Tasks & synchronization

Pass an argument into a worker thread

C++: Thread arguments are copied by default; std::ref explicitly passes a reference.

C++✓ Tested
C++Tasks & synchronization

Protect a shared container with a mutex

C++: A scoped lock_guard releases the mutex when its scope ends.

C++✓ Tested
C++Tasks & synchronization

Retrieve the result of an asynchronous task

C++: The explicit async launch policy requests asynchronous execution rather than deferred evaluation.

C++✓ Tested
C++Tasks & synchronization

Run a task on a thread and join it

C++: Join before reading a result written by the worker thread.

C++✓ Tested
C++Tasks & synchronization

Wait for shared state with a condition variable

C++: Use a predicate so spurious wakeups cannot allow the worker to continue before the state is ready.

C++✓ Tested