Raspberry Pi
Thread2
호타리
2023. 10. 6. 09:57
<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 NULL;
}
void* thread2_main(void *arg)
{
int i;
int cnt = *((int*)arg);
for (i = 0; i < cnt; i++)
{
sleep(1); puts("running thread..2");
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t t1_id, t2_id;
int thread_param = 5;
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;
}
sleep(7); puts("end of main\n");
return 0;
}
<compile 결과>