Setting thread affinity

Hi All,

I am creating some number of threads based on the system processor count for doing repeating task such as file encryption. In this case do I need to set affinity for the threads so that each thread can run on each available processor instead of all of them contending for the same processor. Or I should leave it for the OS to take care the processor allotment for the threads?

Any idea or input greatly appreciated.

Thanks,

Or I should leave it for the OS to take care the processor allotment for the threads?

Unless you have some VERY specific situation (like, for example, an embedded system with all running software known in advance), this is, indeed, the right way to go…

Anton Bassov

As a general rule, setting thread affinity at all is almost always a bad idea. The net effect is merely to reduce the number of opportunities your code has to execute. The Windows scheduler is very, very good, and had been highly tuned over the last three decades.

1 Like

The general rule here is that if you don’t know what affinity to set, than don’t set any affinity at all. The OS usually does a very good job but there are exceptions. and you intrigue me by saying file encryption. is this a hypothetical workload or something that you are actually trying to do?

depending on the implementation, this sort of workload can be in the hard category - where it is alternately IO and CPU bound. and good implementations will use many pending IRPs and queues of buffers but much depends on your exact requirements