Production-grade skill for C++ concurrency and parallel programming. Covers threads, synchronization primitives, atomics, async programming, parallel algorithms, and lock-free data structures.
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
A direct command skips the review prompt. Inspect the source before running it.
Production-grade skill for C++ concurrency and parallel programming. Covers threads, synchronization primitives, atomics, async programming, parallel algorithms, and lock-free data structures.
sasmp_version
1.3.0
skill_version
3.0.0
bonded_agent
01-modern-cpp-expert
bond_type
PRIMARY_BOND
category
development
parameters
{"concurrency_model":{"type":"string","required":false,"enum":["threads","async","coroutines","parallel_stl"],"description":"Concurrency model to use"},"synchronization":{"type":"string","required":false,"enum":["mutex","atomic","lock_free","message_passing"],"description":"Synchronization strategy"},"thread_count":{"type":"string","required":false,"enum":["single","hardware","custom"],"default":"hardware","description":"Thread pool sizing strategy"}}
#include<semaphore>#include<latch>#include<barrier>// Counting semaphorestd::counting_semaphore<10> sem(10); // Max 10 permitsvoidlimited_access(){
sem.acquire(); // Wait for permit// Access limited resource
sem.release(); // Return permit
}
// Latch - single-use barrierstd::latch start_latch(1);
std::latch done_latch(num_workers);
voidworker(){
start_latch.wait(); // Wait for start signal// Do work
done_latch.count_down(); // Signal completion
}
// Barrier - reusable synchronization pointstd::barrier sync_point(num_threads, []() noexcept {
// Called by last thread to arrive (optional)
});
voiditeration_worker(){
for (int i = 0; i < iterations; ++i) {
// Do phase work
sync_point.arrive_and_wait();
}
}
Atomic Operations
Atomic Types
#include<atomic>
std::atomic<int> counter{0};
std::atomic<bool> flag{false};
std::atomic<std::shared_ptr<Data>> shared_data;
// Basic operations
counter.fetch_add(1); // Atomic increment
counter.store(0); // Atomic storeint val = counter.load(); // Atomic load// Compare-and-swapint expected = 0;
bool success = counter.compare_exchange_strong(expected, 1);
// If counter == expected, set to 1 and return true// Otherwise, set expected to current value and return false// Weak version (can fail spuriously, use in loops)while (!counter.compare_exchange_weak(expected, expected + 1)) {
// expected is updated to current value
}
Memory Ordering
// Memory order options (weakest to strongest):// memory_order_relaxed - No synchronization, just atomicity// memory_order_acquire - Prevents reordering after load// memory_order_release - Prevents reordering before store// memory_order_acq_rel - Both acquire and release// memory_order_seq_cst - Total ordering (default)
std::atomic<int> data{0};
std::atomic<bool> ready{false};
// Producervoidproduce(){
data.store(42, std::memory_order_relaxed);
ready.store(true, std::memory_order_release);
// release ensures data store is visible before ready
}
// Consumervoidconsume(){
while (!ready.load(std::memory_order_acquire)) {
// Spin wait
}
// acquire ensures we see data store after readyassert(data.load(std::memory_order_relaxed) == 42);
}
Async Programming
std::async and Futures
#include<future>// Launch async task
std::future<int> result = std::async(std::launch::async, []() {
returnexpensive_computation();
});
// Do other work...// Get result (blocks if not ready)int value = result.get();
// Check if ready without blockingif (result.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
int value = result.get();
}
// Launch policy optionsauto f1 = std::async(std::launch::async, func); // New threadauto f2 = std::async(std::launch::deferred, func); // Lazy evaluationauto f3 = std::async(std::launch::async | std::launch::deferred, func); // Default
Promise and Future
voidworker(std::promise<int> promise){
try {
int result = compute();
promise.set_value(result);
} catch (...) {
promise.set_exception(std::current_exception());
}
}
std::promise<int> promise;
std::future<int> future = promise.get_future();
std::thread t(worker, std::move(promise));
try {
int result = future.get();
} catch (const std::exception& e) {
// Handle exception from worker
}
t.join();
Packaged Task
std::packaged_task<int(int, int)> task([](int a, int b) {
return a + b;
});
std::future<int> result = task.get_future();
// Execute taskstd::thread t(std::move(task), 2, 3);
t.join();
int sum = result.get(); // 5