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;
}