Lock and unlock a mutex
mutex · compared across machine types · 35 Series on 8 machine types · AWS us-east-2
CPU
Section
Idle
Placement
Threads
Order
| Compare | Machine | Variant | P50 | P99 | Unit cost | Runs |
|---|---|---|---|---|---|---|
| c8a.2xlarge | 10.20 ns | — | $0.00000122 /1M ops | 89 | ||
| c8i.2xlarge | 13.64 ns | — | $0.00000142 /1M ops | 89 | ||
| m5.large | 17.51 ns | — | $0.000000467 /1M ops | 91 | ||
| c8g.2xlarge | 17.93 ns | — | $0.00000158 /1M ops | 4 | ||
| c6i.8xlarge | 18.38 ns | — | $0.00000533 /1M ops | 89 | ||
| i4i.2xlarge | 18.39 ns | — | $0.0000035 /1M ops | 89 | ||
| m6id.2xlarge | 18.39 ns | — | $0.00000242 /1M ops | 1 | ||
| i4g.2xlarge | 22.42 ns | — | $0.00000383 /1M ops | 4 |
Compared:m5.largec8g.2xlargec8i.2xlarge
Run-to-run spread
time per critical section across counted RunsOver time
newest counted Runs, time per critical section(one request each to the data API)
What this measures
What a critical section under a pthread mutex costs: uncontended on one thread, and with two threads contending for it on two physical cores or on the two hardware threads of one core.
What it does not measure
- With two threads, the figure is not the time for the lock to pass from one core to the other.
- With
threads1,one_cpuandidle_threads0, the figure is true only of a process that has only ever had one thread, where glibc skips the atomic instructions. A program that has started a thread pays for them, as the same set withidle_threads1 does. - With
threads1,one_cpuandidle_threads1, onc6i.8xlargeandi4i.2xlarge, the Runs counted on 2026-11-25 usually sat at one of two levels from boot to boot, about 30% apart, with one Run between them, and in the Runs examined no recorded fact told the levels apart.
Code & config
Benchmark docsSeries JSONc8i.2xlarge · 1 line · +1 idle · one cpu · 1 thread
148__attribute__((noinline))149static uint64_t critical_sections(shared_t *s, uint64_t target, int lines) {150 pthread_mutex_t *mutex = &s->lock.mutex;151 uint64_t mine = 0;152 for (;;) {153 int rc = pthread_mutex_lock(mutex);// …161 s->lines[0].value = counter + 1;162 for (int i = 1; i < lines; i++) s->lines[i].value++;163 rc = pthread_mutex_unlock(mutex);164 if (__builtin_expect(rc != 0, 0)) lm_die("pthread_mutex_unlock: %s", strerror(rc));165 mine++;// …189static uint64_t mutex_run(void *arg) {190 ctx_t *c = (ctx_t *)arg;191 uint64_t target = c->base + K;192 c->control->target = target;193 uint64_t generation = publish(c, CMD_RUN);194 count(&c->threads[0], critical_sections(c->shared, target, c->lines));// …661 lm_measure(mutex_run, &c);
Configuration
- benchmarkmutex
- buildstatic
- instancec8i.2xlarge
- regionus-east-2
- azuse2-az3
- params.critical_section_lines1
- params.idle_threads1
- params.lockpthread_mutex_default
- params.placementone_cpu
- params.threads1