Recent Posts
printf("ho_tari\n");
Thread3 본문
<main.c>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
void* thread1_main(void *arg)
{
int i;
int cnt = *((int*)arg);
for (i = 0; i < cnt; i++)
{
sleep(1); puts("running thread..1");
}
return (void*)10;
}
void* thread2_main(void *arg)
{
int i;
int cnt = *((int*)arg);
for (i = 0; i < cnt; i++)
{
sleep(1); puts("running thread..2");
}
return (void*)20;
}
int main(int argc, char *argv[])
{
pthread_t t1_id, t2_id;
int thread_param = 2;
int temp = 0;
void* thr_ret;
if (pthread_create(&t1_id, NULL, thread1_main, (void*)&thread_param) != 0)
{
puts("pthread_create() error");
return -1;
}
if (pthread_create(&t2_id, NULL, thread2_main, (void*)&thread_param) != 0)
{
puts("pthread_create() error");
return -1;
}
// 블로킹 함수 (t1_id가 끝날 때까지는 그 밑에 코드는 진행하지 않는다(대기))
if (pthread_join(t1_id, &thr_ret) != 0)
{
puts("pthread_join() error");
return -1;
}
temp = *(int*)&thr_ret;
printf("%d\n", temp);
sleep(3); puts("end of main\n");
return 0;
}
<compile 결과>
'Raspberry Pi' 카테고리의 다른 글
Thread5 (SWITCH_LED_ON_OFF) (0) | 2023.10.06 |
---|---|
Thread4 (0) | 2023.10.06 |
Thread2 (0) | 2023.10.06 |
Thread1 (0) | 2023.10.05 |
SWITCH_LED_INTERRUPT (0) | 2023.10.05 |