0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Windows APIを使って並列処理をする雛形

Posted at
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, &params[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;
}

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?