Four Counters One Line
Four threads, each pinned to its own socket, run this. Each thread gets a distinct id in 0..3.
struct counter { long value; };
static struct counter counters[4]; /* 32 bytes total */
void *worker(void *arg) {
int id = *(int *)arg;
for (long i = 0; i < 500000000L; i++)
counters[id].value++;
return NULL;
}
One thread finishes in 1.6 s. Four threads take 18 s. No thread ever touches another thread's element. More than one of the following removes the slowdown. Which do?
Sign in to answer questions and track your progress
Sign In