Do You Coding?
[function] pthread_create ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
pthread_create - create a new thread
SYNOPSIS
#include <pthread.h>
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
Compile and link with -pthread.
DESCRIPTION
The pthread_create() function starts a new thread in the calling process.
The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of
start_routine(). The new thread terminates in one of the following ways: * It calls pthread_exit(3),
specifying an exit status value that is available to another thread in the same process that calls
pthread_join(3). * It returns from start_routine(). This is equivalent to calling pthread_exit(3) with
the value supplied in the return statement. * It is canceled (see pthread_cancel(3)).
* Any of the threads in the process calls exit(3), or the main thread performs a return from main().
This causes the termination of all threads in the process. The attr argument points to a pthread_attr_t
structure whose contents are used at thread creation time to deโ termine attributes for the new thread;
this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then the thread
is created with default attributes. Before returning, a successful call to pthread_create() stores the ID
of the new thread in the buffer pointed to by thread; this identifier is used to refer to the thread in
subsequent calls to other pthreads functions. The new thread inherits a copy of the creating thread's
signal mask (pthread_sigmask(3)). The set of pending signals for the new thread is empty (sigpending(2)).
The new thread does not inherit the creating thread's alternate signal stack (sigaltstack(2)).
The new thread inherits the calling thread's floating-point environment (fenv(3)).
The initial value of the new thread's CPU-time clock is 0 (see pthread_getcpuclockid(3)).
Linux-specific details
The new thread inherits copies of the calling thread's capability sets (see capabilities(7)) and
CPU affinity mask (see sched_setaffinity(2)).
RETURN VALUE
On success, pthread_create() returns 0;
on error, it returns an error number, and the contents of *thread are undefined.
๐ ํจ์ ์ค๋ช
์ฐ์ , pthread๋, POSIX Thread์ ์ฝ์๋ก, ์ ๋์ค ๊ณ์ด ์ด์์ฒด์ ์์ thread๋ฅผ ๋ง๋ค ์ ์๊ฒ ํ๋ API์ด๋ค.
pthread.h ๋ฅผ include ํด์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, pthread_create๋ฅผ ํตํด thread๋ฅผ ์์ฑํ ์ ์๋ค.
pthread_create ํจ์๋ ํธ์ถ ์ ํด๋น ํ๋ก์ธ์ค์์ ์ ์ค๋ ๋๋ฅผ ์์ฑํ๊ณ , ์ธ์๋ก ๋ฐ์ start_routine ํจ์๋ฅผ ์คํํ๋ค.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
ํจ์์ ๋งค๊ฐ๋ณ์๋ก 4๊ฐ์ ์ธ์๊ฐ ๋ค์ด์ค๋๋ฐ ๊ฐ๊ฐ ์ดํด๋ณด์.
pthread_t *thread : ์์ฑ๋ ์ค๋ ๋์ ID๋ฅผ ์ ์ฅํ ์ค๋ ๋ ์๋ณ์.
์ด ์ธ์ ๊ฐ์ ํตํด์ pthread_join๊ณผ ๊ฐ์ ํจ์๋ค์ ์ฌ์ฉํ๋ค.
const pthread_attr_t *attr : ์ค์ ํ ์ค๋ ๋์ ํน์ฑ ๋ฐ ์์ฑ ์ ๋ณด (NULL ์ ๋ ฅ์ ๊ธฐ๋ณธ์ค์ )
์์ฑ์ ์ง์ ํ๋ ค๊ณ ํ๋ค๋ฉด pthread_attr_init ๋ฑ์ ํจ์๋ก ์ด๊ธฐํํ๋ค.
void *(*start_routine) (void *) : ์ค๋ ๋๊ฐ ์คํํ ํจ์์ ํฌ์ธํฐ (์ค๋ ๋ ์์ฑ ์ ์คํ๋๋ค.)
void *arg : start_routine ํจ์์ ์ ๋ฌํ๊ณ ์ ํ๋ ์ธ์ ๊ฐ
๋จ์ํ๊ฒ, pthread_create ํจ์๋ง์ ์ด์ฉํด์ thread๋ฅผ ํ๋ ์์ฑํด ํจ์ ํ๋๋ฅผ ์คํํ๋ ๊ฒ์ ์ดํด๋ณด๋๋ก ํ๊ฒ ๋ค.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *thread_func(void *arg)
{
int i = 0;
int c = *((int*) arg);
while(i < c)
{
sleep(1);
printf("running thread %d\n", i);
i ++;
}
return (NULL);
}
int main(void)
{
pthread_t thread_ID;
int arg = 5;
if(pthread_create(&thread_ID, NULL, thread_func, (void *)&arg) != 0)
exit(1);
sleep(10);
printf("end\n");
return (0);
}
์ถ๋ ฅ - std_output)

์์ ์ฝ๋๋ main์์ pthread_create ํจ์๋ฅผ ์ฌ์ฉํ์ฌ thread_func ๋ผ๋ ํจ์๋ฅผ ์คํ์ผํ๋ ์ค๋ ๋ ํ๋๋ฅผ ๋ง๋ค ๊ฒ์ด๋ค.
์ด๋ฅผ ํ๋์ฉ ํ์ธํด๋ณด๋ฉด, ์ฐ์ pthread_create์ ์ฒซ ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ pthread_t ๋ก ์ ์ธ๋๋ thread_ID๋ฅผ ๋ง๋ ๋ค.
์ด๋ ์ค๋ ๋ ์๋ณ์๋ก, ์ถํ์ ์ค๋ ๋๋ฅผ ํน์ ํ์ฌ ์๋ณํ ๋ ์ฌ์ฉ๋๋ค.
๊ทธ๋ฆฌ๊ณ , ๋งค๊ฐ๋ณ์๋ก ์ธ arg๋ฅผ ํ๋ ๋ง๋ค๊ณ ์ด๋ฅผ 4๋ฒ์งธ ๋งค๊ฐ๋ณ์์ void *๋ก ์บ์คํ ํด ๋ฃ์ด์ค๋ค.
์ค๋ ๋๊ฐ ์์ฑ์ ์ฑ๊ณตํ๋ฉด 0์ ๋ฐํํ๋ฏ๋ก, 0์ด ์๋ ๋๋ ์ค๋ฅ๋ก exit(1)์ ํด์ฃผ์๊ณ ,
ํจ์๊ฐ ์คํ๋๋ฉด ์์ฒ๋ผ thread_func์ ์คํํ output์ด ์คํ๋๋ค.
main๋ ํ๋์ ์ค๋ ๋์ด๋ฏ๋ก, main ์ค๋ ๋๊ฐ ์ข ๋ฃ๋๋ฒ๋ฆฌ๋ฉด ํ๋ก์ธ์ค ์์ฒด๊ฐ ๋๋๋ฒ๋ฆฌ๊ธฐ ๋๋ฌธ์,
์ฐ๋ฆฌ๊ฐ ๋ง๋ ์ค๋ ๋๊ฐ ์ข ๋ฃ๋ ์ดํ์ main์ด ์ข ๋ฃ๋๋๋ก sleep์ 10 ์ ๋๋ก ๋ฐ๋ก ๊ฑธ์ด๋๋ค.
์ ์ฝ๋๋ฅผ ํตํด์ pthread_create๋ก ํ๋์ ์ค๋ ๋๋ฅผ ๋ง๋ค์ด๋ณด์๋ค.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void *thread_func(void *arg)
{
int i = *((int*) arg);
int c = *((int*) arg) + 5;
while(i <= c)
{
sleep(1);
printf("running thread %d\n", i);
i ++;
}
return (NULL);
}
int main(void)
{
pthread_t thread_ID1;
pthread_t thread_ID2;
int arg1 = 0;
int arg2 = 5;
if(pthread_create(&thread_ID1, NULL, thread_func, (void *)&arg1) != 0)
exit(1);
if(pthread_create(&thread_ID2, NULL, thread_func, (void *)&arg2) != 0)
exit(1);
sleep(10);
printf("ID1 : %ld, ID2 : %ld\n", thread_ID1, thread_ID2);
return (0);
}
์ถ๋ ฅ - std_output)

๊ทธ๋ ๋ค๋ฉด, ์ด๋ฒ์๋ ๋๊ฐ์ ์ค๋ ๋๋ฅผ ํ๋ฒ ๋ง๋ค์ด๋ณด๋๋ก ํ๊ฒ ๋ค.
๊ฐ๊ฐ ๋ค๋ฅธ ID๋ณ์์ ์ค๋ ๋ ์๋ณ์๋ฅผ ์ ์ฅํด์คฌ๊ณ , ๋ค๋ฅธ ID๋ฅผ ๊ฐ์ง์ printํ์ฌ ํ์ธํด๋ณด์๋ค.
๋ ์ค๋ ๋๋ ๋์์ ์คํ๋๋ฉฐ, ๋ง์ฐฌ๊ฐ์ง๋ก main ์ค๋ ๋๋ ๊ธฐ๋ค๋ฆฌ๊ฒ sleep์ ๊ฑธ์ด์คฌ๋ค.
์คํ๊ฒฐ๊ณผ๋ฅผ ํตํด, main ์ค๋ ๋ + ID1์ ๊ฐ์ง๋ ์ค๋ ๋ + ID2๋ฅผ ๊ฐ์ง๋ ์ค๋ ๋
์ธ ๊ฐ์ ์ค๋ ๋๊ฐ ๋์์ ์งํ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์์ฑํ ๋ ์ค๋ ๋์ ์์๋ ๋จผ์ ๋ง๋ ์ค๋ ๋๊ฐ ๋จผ์ ์คํ๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
์ด๋ ์ค๋ ๋ ์ค์ผ์ค๋ง์ ์ํด ์ฐ์ ์์๊ฐ ๋จผ์ ๋ง๋ ์ค๋ ๋์ ๋ถ์ฌ๋ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค.
(์ค๋ ๋ ์ค์ผ์ค๋ง : ์ด๋ค ์์์ ์ํด ์ค๋ ๋๊ฐ ๋ฒ๊ฐ์๊ฐ๋ฉฐ ์คํ๋ ๊ฒ์ธ์ง์ ๋ํ ๊ฒฐ์ ๋ฐฉ์)
์ด๋ ๊ฒ pthread_create ํจ์์ ๋ํด ๊ฐ๋จํ ์์๋ฅผ ํตํด์ ์์๋ณด์๋ค.
์ดํ์ ์ฌ์ฉํ pthread ๊ด๋ จ ํจ์๋ค์ ์ด์ฉํ๊ธฐ ์ ์ค๋ ๋๋ฅผ ์์ฑํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ด ๋๋ ํจ์์๊ณ ,
์์ผ๋ก ์ด๋ค pthread ํจ์๋ค์ ์์๋ณผ ๋ ์ฐ์ ์ ์ผ๋ก ์ฌ์ฉ๋ ๊ฒ์ด๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] pthread_detach ํจ์ ์์๋ณด๊ธฐ (2) | 2024.09.18 |
|---|---|
| [function] pthread_join ํจ์ ์์๋ณด๊ธฐ (8) | 2024.09.07 |
| [utility] get_next_line ๊ตฌํํ๊ธฐ (3) | 2024.08.31 |
| [library] itoa ๊ตฌํํ๊ธฐ (0) | 2024.06.29 |
| [library] split ๊ตฌํํ๊ธฐ (0) | 2024.06.29 |
