#include "stdafx.h" typedef struct { int thread_id; //fill out any other data here } THREAD_CTX; HANDLE *m_threads; int getNumLogicalCPUs() { SYSTEM_INFO si; //make sure you clean everything first ZeroMemory(&si, sizeof(SYSTEM_INFO)); //get all the system info GetSystemInfo(&si); return si.dwNumberOfProcessors; } DWORD workerProc(LPVOID lpParameter); int wmain(int argc, wchar_t* argv[]) { //find out how many cpu's (real, cores, hyperthreading) int cpus = getNumLogicalCPUs(); //create an array to hold a thread handle for each cpu m_threads = new HANDLE[cpus]; wprintf(L"There are %d logical CPUs\r\n", cpus); //create a thread for each cpu for(int i = 0; i < cpus; i++) { wprintf(L"Creating Thread %d\r\n", i); //set up a thread context to carry any data you may need in the thread THREAD_CTX *ctx = new THREAD_CTX; ctx->thread_id = i; //create the thread and store the handle m_threads[i] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)workerProc, (LPVOID)ctx, 0, NULL); wprintf(L"Setting Thread 0x%08p Affinity to CPU%d\r\n", m_threads[i], i); //set each thread to a seperate cpu //this is a bitmask, so its "(1 << cpuno)" SetThreadAffinityMask(m_threads[i], (1 << i)); } return 0; } DWORD workerProc(LPVOID lpParameter) { //retreive the thread context THREAD_CTX *ctx = (THREAD_CTX*)lpParameter; //do some work later return 0; }