C:\Users\test\kaihatsu>gcc test.c -o test
test.c
#include <windows.h>
#include <stdio.h>
#define THREAD_COUNT 4 // スレッド数を4に設定
// スレッドの処理関数
DWORD WINAPI threadFunction(LPVOID lpParam) {
int threadID = *((int*)lpParam);
printf("Thread %d is running on CPU %d\n", threadID, GetCurrentProcessorNumber());
return 0;
}
int main() {
HANDLE threads[THREAD_COUNT];
DWORD threadID[THREAD_COUNT];
int params[THREAD_COUNT];
// スレッドの作成
for (int i = 0; i < THREAD_COUNT; i++) {
params[i] = i; // スレッドごとに識別番号を渡す
threads[i] = CreateThread(NULL, 0, threadFunction, ¶ms[i], 0, &threadID[i]);
if (threads[i] == NULL) {
printf("Failed to create thread %d\n", i);
return 1;
}
}
// スレッドの終了を待機
WaitForMultipleObjects(THREAD_COUNT, threads, TRUE, INFINITE);
// スレッドを閉じる
for (int i = 0; i < THREAD_COUNT; i++) {
CloseHandle(threads[i]);
}
return 0;
}
