目次 - API(機能別) - スレッド - SDL_CreateThread

SDL_CreateThread

新しいスレッドを生成する

構文

SDL_Thread* SDL_CreateThread(SDL_ThreadFunction fn, const char* name, void* data)

引数

fn新しいスレッドで呼ぶ関数 (詳細を参照すること)
nameスレッドの名前 (詳細を参照すること)
datafnへ渡すポインタ

戻り値

成功のとき新しいスレッドオブジェクトへの不透明ポインタ(opaque pointer), 失敗のときNULLを戻す. SDL_GetError()で詳細を知ることができる.

サンプルコード

#include <stdio.h>
#include "SDL_thread.h"

/* とても単純なスレッド - 50ms間隔で0から9までカウントする */
static int TestThread(void *ptr)
{
    int cnt;

    for (cnt = 0; cnt < 10; ++cnt) {
        SDL_Log("スレッドカウンタ: %d\n", cnt);
        SDL_Delay(50);
    }

    return cnt;
}

int main(int argc, char *argv[])
{
    SDL_Thread *thread;
    int         threadReturnValue;

    SDL_Log("単純なSDL_CreateThreadのテスト:\n");

    /* 単にスレッドを生成する */
    thread = SDL_CreateThread(TestThread, "TestThread", (void *)NULL);

    if (NULL == thread) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateThread 失敗: %s\n", SDL_GetError());
    } else {
        SDL_WaitThread(thread, &threadReturnValue);
        SDL_Log("スレッドの戻り値: %d\n", threadReturnValue);
    }

    return 0;
}

出力:

SDL_CreateThreadの簡単なテスト:
スレッドカウンタ: 0
スレッドカウンタ: 1
スレッドカウンタ: 2
スレッドカウンタ: 3
スレッドカウンタ: 4
スレッドカウンタ: 5
スレッドカウンタ: 6
スレッドカウンタ: 7
スレッドカウンタ: 8
スレッドカウンタ: 9
スレッドの戻り値: 10

詳細

これは次の呼び出しと等価である:

SDL_CreateThreadWithStackSize(fn, name, 0, data);

バージョン

SDL 2.0.0以降

関連項目(関数)

SDL_CreateThreadWithStackSize
SDL_WaitThread

SDL Wikiへのリンク

SDL_CreateThread - SDL Wiki