地方エンジニアの学習日記

興味ある技術の雑なメモだったりを書いてくブログ。たまに日記とガジェット紹介。

【C】pthreadsのテンプレ

スレッド使ったサンプル作るためのテンプレ

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_func(void *param);

int main(void) {

    pthread_t thread;
    int ret = 0;

    ret = pthread_create(&thread, NULL, thread_func, NULL);
    if (ret != 0) {
        // pthread_createが失敗した場合は0以外の値を返す
        exit(1);
    }

    // スレッドの終了を待機
    // int pthread_join(pthread_t th, void **thread_return)
    ret = pthread_join(thread, NULL);
    if (ret != 0) {
        // pthread_joinが失敗した場合は0以外の値を返す
        exit(1);
    }

    /*
    ret = pthread_detach(thread);
    if (ret != 0) {
        // pthread_detachが失敗した場合は0以外の値を返す
        exit(1);
    }
    */

    return EXIT_SUCCESS;
}

void *thread_func(void *param)
{
    // 非同期に実行する処理
}