Creative Code

03_THREAD.c 본문

Raspberry PI(C)

03_THREAD.c

빛하루 2023. 10. 6. 09:37
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

// 스레드에서 실행되는 함수
void* thread_main(void *arg)
{
    int i;
    int cnt = *((int*)arg);

    for (i = 0; i < cnt; i++)
    {
        sleep(1);
        puts("running thread\n"); // 스레드에서 실행 중인 메시지 출력
    }

    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t t_id;
    int thread_param = 5;

    // 스레드 생성
    if (pthread_create(&t_id, NULL, thread_main, (void*)&thread_param) != 0)
    {
        puts("pthread_create() error\n");
        return -1;
    }

    sleep(7);
    puts("end of main\n"); // 메인 스레드에서 실행 중인 메시지 출력

    return 0;
}

'Raspberry PI(C)' 카테고리의 다른 글

06_THREAD3.c  (0) 2023.10.06
05_THREAD2.c  (0) 2023.10.06
04_THREAD1.c  (0) 2023.10.06
02_SWITCH_LED_INTERRUPT.c  (0) 2023.10.06
01_LED_ON_OFF.c  (0) 2023.10.06