2018년 3월 19일 월요일

thread - pthread, win32 thread

Thread API 종류

  • pthread
  • win32 thread API


pthread


#include <pthread.h>


void * foo(void * args) {
    // code



int retcode;
pthread_t handle;
if (pthread_create(&handle, NULL, foo, NULL) != 0) {
    // error
}

pthread_join(handle, (void*)&retcode);


detached thread


void * foo(void * args) {
    // code
}


pthread_t handle;
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0) {
    printf("pthread_attr_init() error\n");
    exit(1);
}

if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
    printf("pthread_attr_setdetachstate() error\n");
    exit(1);
}

if (pthread_create(&handle, &attr, foo, NULL) != 0) {
    printf("pthread_create() error\n");
    exit(1);
}

pthread_attr_destroy(&attr);



win32 thread API


#define WIN32_LEAN_AND_MEAN
#define _WINSOCKAPI
#include <Windows.h>
#include <process.h>


UINT WINAPI foo (void * args) {
    // code
}


UINT id;
HANDLE handle;
handle = (HANDLE)_beginthreadex(NULL, 0, foo, NULL, 0, &id);


댓글 없음:

댓글 쓰기