cpp_pthread - 8BitsCoding/RobotMentor GitHub Wiki
์ฐธ๊ณ , ๊ฐ๋ฐํ๊ฒฝ์ ๋ฆฌ๋ ์ค์์ ์คํํด์ pthread๋ฅผ ์ฌ์ฉํจ
- ๊ถ๊ธํ๋ ์ ์ด mutex์ ๋ฐฉ์ด? ๋ฒ์๊ฐ ์ด๋๊น์ง์ธ์ง๊ฐ ๊ถ๊ธํ์ -> ๊ฒฐ๋ก ์ mutex๊ฐ ์ ์ธ๋ ์ธ์คํด์ค๊น์ง ๋ฐฉ์ด๋ฒ์๊ฐ ๋๋ค. ์๋ ์ฝ๋๋ฅผ ์ฐธ์กฐ
#include <pthread.h>
#include <unistd.h>
class B;
class A{
public:
//pthread_t p;
int thr_id;
int count = 0;
B* b;
void setB(B* _b) {b = _b;}
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// mutex๊ฐ A์ ์ ์ธ๋์๊ธฐ์ A์ธ์คํด์ค ๋ด๋ถ๋ง ๋ฐฉ์ด๊ฐ ๋๋ค.
void startThr() {
//thr_id = pthread_create(&p, NULL, &A::threadA, (void*)&thr_id);
}
};
class B{
public:
//pthread_t p;
int thr_id;
int count = 0;
A* a;
void setA(A* _a) {a = _a;}
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void startThr() {
//thr_id = pthread_create(&p, NULL, &B::threadB, (void*)&thr_id));
}
};
void* threadA(void* p){
B* b = (B*)p;
while(1)
{
pthread_mutex_lock(&(b->mutex)); // 2๋ฒ ํธ์ถ๋๋ฉด unlock ํธ์ถ ์ ๊น์ง ์ฌ๊ธฐ์ ๋๊ธฐ
// ์ฌ๊ธฐ๊ฐ ์ ์ผ ์ค์ํ๋ฐ, ์ด๊ฒ ๋ฌด์จ๋ง์ด๋๊ณ ํ๋ฉด mutex lock์ด ๊ฑธ๋ ค์๋ ๋์ B ์ธ์คํด์ค์ ๋ณ์๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ ๊ถํ์ ํ์ฌ ์ค๋ ๋ ๋ฐ์ ์๋ค๋ ์ .
// ๋ฐ๋ผ์ ๋ค๋ฅธ ์ค๋ ๋(ํน์ ์๊ธฐ ์์ ์ด๋ผ๋) ์ ๊ทผํ๋ ค๊ณ ํ ์ ์๊ณ ๋๊ธฐํด ์๊ฒ ๋๋ค.
printf("A : %d\n", (b->a->count)++);
//b->a->count++;
sleep(2);
pthread_mutex_unlock(&(b->mutex));
}
}
void* threadB(void* p){
B* b = (B*)p;
while(1)
{
//pthread_mutex_lock(&(b->mutex));
printf("threadB to a : %d\n", (b->a->count)++);
sleep(1);
//pthread_mutex_unlock(&(b->mutex));
}
}
int main(int argc, char **argv)
{
A* a = new A;
B* b = new B;
a->setB(b);
b->setA(a);
pthread_t p[2];
a->thr_id = pthread_create(&p[0], NULL, threadA, (void*)a);
b->thr_id = pthread_create(&p[1], NULL, threadB, (void*)b);
threadA์์ B๋ฅผ mutex lock๊ฑธ์ด๋ฒ๋ฆฌ๊ณ threadB์์ B์ธ์คํด์ค์ ์ ๊ทผํ๋ค.
-> ์ ๋ต์ ์ ๊ทผ์ด ๋๋ค. ๋งค์ฐ ์ํํ๊ฒ ์ง?? -> ๊ฒฐ๋ก )) mutex๋ฅผ ์ ๋ง ์ ์ฌ์ฉํด์ผํ๋ค.!!!
void* threadA(void* p){
B* b = (B*)p;
while(1)
{
pthread_mutex_lock(&(b->mutex)); // 2๋ฒ ํธ์ถ๋๋ฉด unlock ํธ์ถ ์ ๊น์ง ์ฌ๊ธฐ์ ๋๊ธฐ
printf("A : %d\n", (b->count)++);
sleep(2);
//pthread_mutex_unlock(&(b->mutex));
}
}
void* threadB(void* p){
B* b = (B*)p;
while(1)
{
//pthread_mutex_lock(&(b->mutex));
printf("threadB to b : %d\n", (b->count)++);
sleep(1);
//pthread_mutex_unlock(&(b->mutex));
}
}
#include <pthread.h>
// ์ฐ๋ ๋ ํจ์
void *t_function(void * data) {
int id;
int i = 0;
id = *((int*)data);
while(1)
{
printf("%d : %d\n", id, i);
i++;
sleep(1);
}
}
// ์ฐ๋ ๋ ์ฌ์ฉ
int main() {
pthread_t p_thread[2];
int thr_id;
int status;
int a = 1;
int b = 2;
// ์ฐ๋ ๋ ์์ฑ ํ ์๊ท๋จผํธ๋ฅผ 1๋ก ๋๊ธด๋ค.
thr_id = pthread_create(&p_thread[0], NULL, t_function, (void*)&a);
if(thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
// ์ฐ๋ ๋ ์์ฑ ํ ์๊ท๋จผํธ๋ฅผ 2๋ก ๋๊ธด๋ค.
thr_id = pthread_create(&p_thread[1], NULL, t_function, (void*)&b);
if(thr_id < 0)
{
perror("thread create error : ");
exit(0);
}
// ์ฐ๋ ๋๊ฐ ์ข
๋ฃ๋๊ธฐ๋ฅผ ๊ธฐ๋ค๋ฆฐ๋ค. (๋ฃ์ง ์์ ์ Main ์ฐ๋ ๋๊ฐ ๋จผ์ ์ข
๋ฃ๋๊ธฐ์ ํ
์คํ
์ด ๋ถ๊ฐ๋ฅ!)
pthread_join(p_thread[0], (void**)&status);
pthread_join(p_thread[1], (void**)&status);
// (์ฐธ๊ณ ) pthread_join์ ๋ํ ์ค๋ช
: join์ ํ ์ ํด๋น ์ฐ๋ ๋๊ฐ ์ข
๋ฃ ๋ ๋๊น์ง ๋ค์ ๋ผ์ธ์ผ๋ก ๋์ด๊ฐ์ง ์์
/*
pthread_join(p_thread[0], (void**)&status);
printf("End Thread \n");
*/
// p_thread[0]์ด ์ข
๋ฃ๋๊ณ ๋์ printf๊ฐ ํธ์ถ๋๋ค.
return 0;
}
pthread_detach(p_thread[0]);
// main ์ฐ๋ ๋์์ p_thread[0]์ ์ปจํธ๋กค ํ ์ ์๊ฒ๋๋ค.
// ๊ฑฐ์ ์ฌ์ฉํ ์ผ์ด ์๋ค๊ณ ๋ณด๋ฉด๋จ.
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int ncount; // ์ฐ๋ ๋๊ฐ ๊ณต์ ๋๋ ์์
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // ์ฐ๋ ๋ ์ด๊ธฐํ
// ์ฐ๋ ๋ ํจ ์ 1
void* do_loop(void *data)
{
int i;
pthread_mutex_lock(&mutex); // ์ ๊ธ์ ์์ฑํ๋ค.
for (i = 0; i < 10; i++)
{
printf("loop1 : %d", ncount);
ncount ++;
sleep(1);
}
pthread_mutex_unlock(&mutex); // ์ ๊ธ์ ํด์ ํ๋ค.
}
// ์ฐ๋ ๋ ํจ์ 2
void* do_loop2(void *data)
{
int i;
// ์ ๊ธ์ ์ป์ผ๋ ค๊ณ ํ์ง๋ง do_loop ์์ ์ด๋ฏธ ์ ๊ธ์
// ์ป์์์ผ๋ก ์ ๊ธ์ด ํด์ ๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
pthread_mutex_lock(&mutex); // ์ ๊ธ์ ์์ฑํ๋ค.
for (i = 0; i < 10; i++)
{
printf("loop2 : %d", ncount);
ncount ++;
sleep(1);
}
pthread_mutex_unlock(&mutex); // ์ ๊ธ์ ํด์ ํ๋ค.
}
int main()
{
int thr_id;
pthread_t p_thread[2];
int status;
int a = 1;
ncount = 0;
thr_id = pthread_create(&p_thread[0], NULL, do_loop, (void *)&a);
sleep(1);
thr_id = pthread_create(&p_thread[1], NULL, do_loop2, (void *)&a);
pthread_join(p_thread[0], (void *) &status);
pthread_join(p_thread[1], (void *) &status);
status = pthread_mutex_destroy(&mutex);
printf("code = %d", status);
printf("programing is end");
return 0;
}
- http://ww.sourceware.org/pthreads-win32 ์ ๋ค์ด๊ฐ์ pthread lib, include, dll์ ๋ค์ด ๋ฐ๋๋ค.
- 'prebuilt-dll-2-9-1'์ ์ถ์
- ํ๋ก์ ํธ์ C/C++ -> ์ผ๋ฐ -> ์ถ๊ฐ์ข ์์ฑ -> include๋ฅผ ์ถ๊ฐ
- ํ๋ก์ ํธ์ ๋ง์ปค -> ์ผ๋ฐ -> ์ถ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ -> lib\x##์ ์ถ๊ฐ
- ํ๋ก์ ํธ์ ๋ง์ปค -> ์ ๋ ฅ -> ์ถ๊ฐ ์ข ์์ฑ -> lib\x##์ lib๋ฅผ ์ถ๊ฐ
- Windows\SysWOW64์ dll์ ์ถ๊ฐ
์ฐธ๊ณ ์ฌ์ดํธ
์ฐธ๊ณ ์ฌ์ดํธ2
Windowsํ๊ฒฝ pthread์ฌ์ฉ