X

Binding to the current processor

Guest Author

Just hacked up a snippet of code to stop a thread migrating to a different CPU while it runs. This should help the thread get, and keep, local memory. This in turn should reduce run-to-run variance.

#include <sys/processor.h>
void bindnow()
{
processorid_t proc = getcpuid();
if (processor_bind(P_LWPID, P_MYID, proc, 0))
{ printf("Warning: Binding failed\n"); }
else
{ printf("Bound to CPU %i\n", proc); }
}

Join the discussion

Comments ( 2 )
  • Carlos Azevedo Friday, May 31, 2013

    On the other hand, I wonder if that exact processor isn't available, the process wouldn't be put into sleep. Would it?


  • Darryl Gove Friday, May 31, 2013

    The thread would wait until it could get onto that exact processor. So binding is in general, not a good idea. However, it can be very useful if you control the machine and can place the threads exactly where you want them. The scenario I most often need is when I want to control every variable so that I can be sure that a run-to-run difference is due to something I've done rather than random chance.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.