linux-sysprog · beginner · ~15 min
Tell race-safe operations from racy ones.
A plain x++ is three steps (load, add, store) that can interleave across threads; atomics and mutexes make it indivisible. Classify an operation as race-safe or not.
Implement int op_is_safe(const char *op) that returns 1 if op names a race-safe operation, else 0.
op: an operation name string.1 for "atomic_fetch_add" or "mutex_guarded"; 0 for "plain_plusplus".
op_is_safe("atomic_fetch_add") -> 1
op_is_safe("mutex_guarded") -> 1
op_is_safe("plain_plusplus") -> 0
op: an operation name string.
1 for atomic_fetch_add/mutex_guarded, else 0.
plain_plusplus is racy; the others are safe.
#include <string.h>
int op_is_safe(const char *op) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.