rosieblue
article thumbnail
728x90

๋…ธ์…˜์— ์ •๋ฆฌํ•˜๋ ค๋‹ค๊ฐ€ ๊ทธ๋ƒฅ ๋ธ”๋กœ๊ทธ์— ์ •๋ฆฌํ•œ๋‹ค..

๊ฐ‘์ž๊ธฐ ์ด๊ฑธ ํ•˜๋Š” ์ด์œ ๋Š” ๋žฉ์—์„œ ๋‚˜ํ•œํ…Œ ํ•˜๋ผ๊ณ  ํ•˜์‹  ์ผ์ด ์Šค๋ ˆ๋“œ ๊ด€๋ จ์ด์–ด์„œ ๋จผ์ € ํ”„๋กœ๊ทธ๋ž˜๋ฐํ•˜๋Š” ๊ฒƒ์ข€ ์ œ๋Œ€๋กœ ์•Œ์•„๋ณด๊ณ  ์ผ์„ ํ•˜๋ ค๊ณ  ํ•œ๋‹ค..

 

์ฐธ๊ณ ๋กœ pthread๊ด€๋ จ ์ฒ˜๋ฆฌ๋ฅผ ํ•  ๋•Œ, 'undefined reference to `pthread_create' ์ด๋Ÿฐ ์˜ค๋ฅ˜๊ฐ€ ๋‚  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ์ด๊ฑธ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ์ปดํŒŒ์ผ ํ•  ๋•Œ ์•„๋ž˜์ฒ˜๋Ÿผ -lpthread ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค!

 gcc -o pthread pthread.c -lpthread

 

 

  • pthread_create ํ•จ์ˆ˜ 
 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg);

/*
์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ: ์“ฐ๋ ˆ๋“œ ์‹๋ณ„์ž (์Šค๋ ˆ๋“œ ์ฃผ์†Œ)
๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ: ์“ฐ๋ ˆ๋“œ ํŠน์„ฑ (๊ธฐ๋ณธ์€ NULL)
์„ธ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ: ๋ถ„๊ธฐ์‹œ์ผœ์„œ ์‹คํ–‰ํ•˜๊ฒŒ ๋  ์“ฐ๋ ˆ๋“œ ํ•จ์ˆ˜์˜ ์ฃผ์†Œ
๋„ค๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ: ์“ฐ๋ ˆ๋“œ ํ•จ์ˆ˜๊ฐ€ ๋„˜๊ฒจ๋ฐ›์„ ๋งค๊ฐœ๋ณ€์ˆ˜

์„ฑ๊ณตํ•˜๋ฉด 0 ์•„๋‹ˆ๋ฉด ์ด์ƒํ•œ ๊ฐ’ ๋ฐ˜ํ™˜ใ…‡ใ…‡
*/

 

์ด ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šค๋ ˆ๋“œ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  3๋ฒˆ์งธ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋œ๋‹น 

 

์˜ˆ์ œ)

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void *p_function(void *data){ //funcion pointer

pid_t pid; //process id
pthread_t tid; //thread id

pid=getpid();
tid=pthread_self();

char*thread_name=(char*)data;
int i=0;

while(i<3){
        printf("thread name:%s, tid:%x, pid:%d\n",thread_name,(unsigned int)tid,(unsigned int)pid);
        i++;
        sleep(1);
}
}

int main(){
pthread_t pthread[2]; //arrary storing pthreads
int thr_id;
int status;
char p1[]="th1";
char p2[]="th2";
char main_[]="main";

thr_id=pthread_create(&pthread[0],NULL,p_function,p1);
printf("Return Val: %d\n",thr_id);
if (thr_id!=0){
        perror("Failed to create pthread!\n");
        exit(EXIT_FAILURE);
}

p_function(main_);

thr_id=pthread_create(&pthread[1],NULL,p_function,p2);
printf("Return Val: %d\n",thr_id);
if(thr_i!=0){
        perror("Failed to creae pthread!\n");
        exit(EXIT_FAILURE);
}
return 0;
}

 

์ด๊ฑธ ๋Œ๋ฆฌ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๊ทธ๋ƒฅ thread2๋ฅผ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ๋๋‚˜๋ฒ„๋ฆฐ๋‹น......

 

๊ทธ๋ž˜์„œ ์ด๋•Œ pthread_joinํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‚จ์€ ์Šค๋ ˆ๋“œ๋“ค์˜ ์‹คํ–‰์„ ๊ธฐ๋‹ค๋ ค์ค„ ์ˆ˜ ์žˆ๋‹น

 

  • pthread_join ํ•จ์ˆ˜ 
int pthread_join(pthread_t thread, void ** return);

/*
(thread๊ฐ€ ์ข…๋ฃŒํ•˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋‹ค๊ฐ€ ์ข…๋ฃŒํ•˜๋ฉด ํฌ์ธํ„ฐ๋ฅผ ๋ฆฌํ„ด)
์ฒซ๋ฒˆ์งธ ์ธ์ž: thread ๊ฐ์ฒด
๋‘๋ฒˆ์งธ ์ธ์ž: NULL์ด ์•„๋‹ˆ๋ฉด ํฌ์ธํ„ฐ ๊ฐ’์„ return ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
*/

 

 

pthread_join(pthread[0],&status);

pthread_join(pthread[1],&status);

์ด๋Ÿฐ์‹์œผ๋กœ ํ•ด์„œ ์Šค๋ ˆ๋“œ๋“ค์„ ๋ฉ”์ธ ํ•จ์ˆ˜๊ฐ€ ๊ธฐ๋‹ค๋ ค์ค„ ์ˆ˜ ์žˆ๋‹ค!

 

 

์œ„ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์‚ฌ์ง„์ฒ˜๋Ÿผ th2๊นŒ์ง€ ๋‹ค ๊ธฐ๋‹ค๋ ค์ฃผ๊ณ  ์ข…๋ฃŒ๋œ๋‹ค~

 

์ฐธ๊ณ ๋กœ ๋‹น์—ฐํ•˜์ง€๋งŒ pid๋Š” ๋‹ค ๊ฐ™์€ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค!

 

์œผ์•… ๊ท€์ฐจ๋‚ญ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค์€ ๋‹ค๋ฅธ ๊ฒŒ์‹œ๋ฌผ์—์„œ ์“ฐ๊ฒ ๋‹น~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

 

 

Reference

https://m.blog.naver.com/whtie5500/221692793640

 

[C] pthread๋ž€? pthread์˜ˆ์ œ

์˜ค๋Š˜์€ pthread์ž…๋‹ˆ๋‹ค! ์•„๋งˆ ์šด์˜์ฒด์ œ์‹œ๊ฐ„์— ๋ฐฐ์šธํ…๋ฐ์š”~ ์ €๋Š” ์šด์˜์ฒด์ œ์ˆ˜์—…์‹œ๊ฐ„์— ๋ฐฐ์› ๊ฑฐ๋“ ์š”! ํ  ์˜ค๋Š˜์€ ...

blog.naver.com

https://www.crocus.co.kr/484

 

์†Œ์ผ“ ํ”„๋กœ๊ทธ๋ž˜๋ฐ - (17) Pthread 5๊ฐ€์ง€ ์˜ˆ์ œ ์ฝ”๋“œ

- ๋ณธ ๋‚ด์šฉ์€ Linux (Ubuntu 14.04 lts)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค. - 1. Create์™€ join์„ ์ด์šฉํ•œ ์ฝ”๋“œ - ๊ธฐ๋ณธ ๋™์ž‘ ๊ณผ์ • :: 1. threadID๋ผ๋Š” ์Šค๋ ˆ๋“œ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑ2. ์Šค๋ ˆ๋“œ ์ƒ์„ฑ, ์ด๋•Œ threadID๋ฅผ ์ธ์ž๋กœ ๋ณด๋‚ด์–ด TID๋ฅผ

www.crocus.co.kr

https://www.geeksforgeeks.org/thread-functions-in-c-c/

 

Thread functions in C/C++ - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

 

profile

rosieblue

@Rosieblue

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!