3 ms·
> These code samples measure context switching overheads using two different techniques: > 1. A pipe which is used by two threads to ping-pong a tiny amount of
by tzs 8y ago
> These code samples measure context switching overheads using two different techniques:
> 1. A pipe which is used by two threads to ping-pong a tiny amount of data. Every read on the pipe blocks the reading thread, and the kernel switches to the writing thread, and so on.
> 2. A condition variable used by two threads to signal an event to each other.
Another technique I've seen used was to have some shared memory with several threads running loops that just copy the CPU time stamp counter to the shared memory. Call these the "writer" threads.
Another group of threads (the "readers") run loops that read the counter, subtract the value from shared memory, and whenever that difference is lower than any previous difference they have seen print it. Each reader prints a decreasing sequence of times bounded below by the minimal thread switch time.