你可以通过编写例程在你的程序里面。
然而,你不应该这样做,因为操作系统是管理这些东西的最佳人选。我的意思是用户模式程序不应该尝试去做。但是,有时候,它可以完成(对于真正的高级用户)来实现负载均衡,甚至可以找出真正的多线程多核问题(数据竞争/缓存一致性...),因为不同的线程将是真正的在不同处理器上执行。
话虽如此,如果您仍想实现,我们可以通过以下方式完成。我为您提供了(Windows操作系统)的伪代码,但是它们也可以轻松地在Linux上完成。
#define MAX_CORE 256
processor_mask[MAX_CORE] = {0};
core_number = 0;
Call GetLogicalProcessorInformation();
// From Here we calculate the core_number and also we populate the process_mask[] array
// which would be used later on to set to run different threads on different CORES.
for(j = 0; j < THREAD_POOL_SIZE; j++)
Call SetThreadAffinityMask(hThread[j],processor_mask[j]);
//hThread is the array of handles of thread.
//Now if your number of threads are higher than the actual number of cores,
// you can use reset the counters(j) once you reach to the "core_number".
上述程序被调用后,线程将总是以下面的方式执行:
Thread1-> Core1
Thread2-> Core2
Thread3-> Core3
Thread4-> Core4
Thread5-> Core5
Thread6-> Core6
Thread7-> Core7
Thread8-> Core8
Thread9-> Core1
Thread10-> Core2
...............
有关详细信息,请参阅手动/ MSDN更多地了解这些概念。
如果你的避风港”没有.NET? – Crashworks 2009-02-20 00:59:57
这是一个.NET相关的问题。为什么你没有.NET? – 2009-03-03 20:49:57