Keep total work constant
Use a C compiler and POSIX threads on Linux. Each run performs exactly four hundred thousand increments in total, divided across one, two or four threads. Giving every thread four hundred thousand increments would change total work at the same time as concurrency. All updates acquire the same mutex, so the protected counter operation itself has no parallel execution. Check the final counter separately from timing to ensure that a faster run has not simply lost updates.
Compile and run the measured experiment
Save the code as mutex.c, compile with cc -O2 -pthread mutex.c -o mutex, and run the executable. The helper checks pthread return codes, and a failed run must not be counted as a valid timing measurement. A barrier aligns the start, while a monotonic clock measures each run through thread completion. The printed seconds are measurements of that environment, not predetermined expected answers. Record CPU allocation, container constraints and competing load, and repeat runs to observe variability before drawing conclusions.
#define _XOPEN_SOURCE 700
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
static void check(int rc) {
if (rc != 0) { fprintf(stderr,"pthread error %d\n",rc); exit(1); }
}
static pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
static pthread_barrier_t gate;
static unsigned long counter, loops;
static void wait_gate(void) {
int rc = pthread_barrier_wait(&gate);
if (rc != PTHREAD_BARRIER_SERIAL_THREAD) check(rc);
}
static void *run(void *arg) {
(void)arg;
wait_gate();
for (unsigned long i=0; i<loops; i++) {
check(pthread_mutex_lock(&mu));
counter++;
check(pthread_mutex_unlock(&mu));
}
return NULL;
}
int main(void) {
for (int n=1; n<=4; n*=2) {
pthread_t threads[4]; struct timespec a,b;
counter=0; loops=400000UL/n;
check(pthread_barrier_init(&gate,NULL,n+1));
for(int i=0;i<n;i++) check(pthread_create(&threads[i],NULL,run,NULL));
if (clock_gettime(CLOCK_MONOTONIC,&a) != 0) return 3;
wait_gate();
for(int i=0;i<n;i++) check(pthread_join(threads[i],NULL));
if (clock_gettime(CLOCK_MONOTONIC,&b) != 0) return 3;
if (counter != 400000) return 2;
printf("threads=%d counter=%lu seconds=%.6f\n",n,counter,
(b.tv_sec-a.tv_sec)+(b.tv_nsec-a.tv_nsec)/1e9);
check(pthread_barrier_destroy(&gate));
}
return 0;
}Interpret contention without overclaiming
More threads cannot execute counter++ simultaneously while it is protected by one mutex. Waiting, scheduling and movement of the shared cache line can add overhead, so additional threads may make the fixed amount of work slower. Nevertheless, this lab does not promise monotonically increasing times on every machine. Placement, frequency changes, mutex implementation and short-run variation affect results. Timing alone also does not reveal the precise number of kernel blocking operations; use additional measurements to test that hypothesis if needed.
Compare a design with less shared state
A useful follow-up gives each worker a local counter and combines results only after joining. That changes the sharing requirement rather than attempting to make a contended mutex magically parallel. Take care that local counters do not introduce false sharing and that compiler simplification does not remove the intended work. This tiny increment benchmark does not predict the scaling of an I/O-heavy service. Use it as a controlled starting point for investigating lock granularity and critical-section duration.
Do not create a supposedly faster comparison simply by removing the mutex around an ordinary shared C variable. Concurrent unsynchronized increments introduce a data race and undefined behavior. Compare a correct per-worker design or a suitable atomic counter as a separately specified experiment. Different synchronization mechanisms can have different semantics, and timing an incorrect implementation does not establish an optimization over the protected version.