<div>안녕하세요 !<br>운영체제 공부중인 학생입니다.<br>concurrent program에 관련 문제에 대해 질문 드립니다</div> <div> </div> <div>int Gv;<br>void* AddGv (void *ptr)<br>{<br> Gv = Gv + 1;<br> pthread_exit(0);<br>}<br>int main()<br>{<br> pthread_t th1, th2;<br> Gv = 0;<br><strong> pthread_create (&th1, NULL, AddGv, NULL);<br> pthread_create (&th2, NULL, AddGv, NULL);</strong></div> <div><strong> pthread_join (th1, NULL);</strong></div> <div><strong> pthread_join (th2, NULL);</strong><br> printf("Result = %d\n",Gv);</div> <div> return 0;<br>}</div> <div> </div> <div>라는 코드에서</div> <div>쓰레드간 동기화문제 때문에 </div> <div>Result가 2가 나오지 않을 경우를 배제하기 위한 코드를 제시하는것이 문제입니다.</div> <div> </div> <div>그런데 굵게 표시된 부분에서 pthread_join 함수를 이용하면 해당 쓰레드가 종료될때까지 코드가 진행되지 않으니</div> <div> </div> <div> pthread_create (&th1, NULL, AddGv, NULL);</div> <div> pthread_join (th1, NULL);<br> pthread_create (&th2, NULL, AddGv, NULL);</div> <div> pthread_join (th2, NULL);</div> <div> </div> <div>로 단순히 고치면 되는것 아닌가요??</div> <div> </div> <div>굳이 mutex함수를 써서 동기화를 제어해야 하는 이유가 무엇인지 궁금합니다.</div>
댓글 분란 또는 분쟁 때문에 전체 댓글이 블라인드 처리되었습니다.