It's a good question. Firsrly, I like your code to set CPU mask and lock pages. But instead of sched_setaffinity(0,1,&set); there should be more correct to use sched_setaffinity(0,sizeof(set),&set);
As to instability on the first measurements, they are in my opinion, due to CPU instruction caches or pipe-line init. But in general your code is run in OS environment and can be interupted by other devices/demons like timer, IO*, eth0* etc. Look at /proc/interrupts on Linux to see CPU-interupts. So fluctuations in time-stamp-counters are possible on the same piece of code. Therefore, I'd suggest using kind of statistical analisys of resuts to get predictable performance.
For example, I got the following on my machine: t2=4711 t2=4683 t2=4683 t2=4683 t2=4683 t2=4683 t2=4683 t2=4683 t2=18123 t2=4683 t2=4683 t2=4683 t2=4683 t2=4676 t2=4683 t2=4676 t2=4683 t2=4683 t2=4683 t2=4683
Sorry for the late answer but I didn't saw it before now. (Is there a way to get mail notice of message follow-up?)
There is a check box next to "Subscribed to this Thread" near the top of the page. If youclick onthis check box, you will get an email notification whenever someone is posting a new answer to this thread.