[c언어] C 언어에서의 스레드 생성 함수

C 프로그래밍에서 스레드를 생성하려면 pthread_create 함수를 사용합니다. 이 함수는 POSIX 스레드(pthread) 라이브러리에서 제공됩니다.

pthread_create 함수

pthread_create 함수는 아래와 같은 형식을 갖습니다:

#include <pthread.h>

int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);

pthread_create 함수가 성공하면 0을, 실패하면 오류 코드를 반환합니다.

예제

아래는 간단한 C 프로그램입니다. 이 예제에서는 pthread_create 함수를 사용하여 새로운 스레드를 생성하고 실행합니다.

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

void *thread_function(void *arg) {
    printf("This is a new thread\n");
    return NULL;
}

int main() {
    pthread_t tid;
    pthread_create(&tid, NULL, thread_function, NULL);

    printf("This is the main thread\n");
    pthread_join(tid, NULL);

    return 0;
}

이 예제는 pthread_create 함수를 사용하여 새로운 스레드를 생성하고 thread_function 함수를 실행한 후, 메인 스레드와 새로운 스레드를 조인합니다.

참고

C 언어에서의 pthread_create 함수를 사용하여 스레드를 생성하는 방법에 대해 간단히 소개했습니다. 추가적인 자세한 내용은 POSIX 스레드 문서나 관련 서적을 참고하시기 바랍니다.