[질문] pthread win32를 이용한 예제결과 의문점
글쓴이: bourne / 작성시간: 월, 2009/02/09 - 11:01오후
#include<stdio.h> #include<stdlib.h> #include<pthread.h> void* do_loop(void* data) { int i=0; int j=0; int me = *((int*)data); /* 쓰레드 구분 숫자 */ for (i=0; i<5; i++) { for (j=0; j<100000000; j++) /* 지연 루프 */ ; printf("'%d' - Got '%d'\n", me, i); } } int main(int argc, char* argv[]) { int thread_id1; int thread_id2; pthread_t pthread1; pthread_t pthread2; int a = 1; /* 1번 쓰레드 구분 숫자 */ int b = 2; /* 2번 쓰레드 구분 숫자 */ int c = 3; thread_id1 = pthread_create(&pthread1, NULL, do_loop, (void*)&a); thread_id2 = pthread_create(&pthread2, NULL, do_loop, (void*)&b); /* main()함수에서도 'do_loop()' 실행시키기 */ do_loop((void*)&c); pthread_join(pthread1, NULL); pthread_join(pthread2, NULL); return 0; }
위의 코드는 웹에서 찾은 것입니다.
window 환경에서 위의 코드를 실행했을때 올바른 결과가 출력될 때도 있지만,
아래와 같이 잘못된 결과를 출력할 때도 있습니다.
==============================================
'1' - Got '0' <-- 동일한 결과가 두번 출력
'1' - Got '0' <-- 동일한 결과가 두번 출력
'3' - Got '0'
'3' - Got '1'
'1' - Got '1'
'3' - Got '2'
'1' - Got '2'
'3' - Got '3'
'2' - Got '0'
'1' - Got '3'
'3' - Got '4'
'2' - Got '1'
'1' - Got '4'
'2' - Got '2'
'2' - Got '3'
'2' - Got '4'
Press any key to continue
==============================================
무엇이 문제인지 모르겠습니다.
이것을 토대로 다른 프로그램에 적용해 보려고 했는데,
예제부터 잘못된 결과가 출력되니 당황스럽네요...
도움부탁드립니다.
Forums:
믿어지지가
믿어지지가 않는데요.
제가 직접 해봐도 리눅스 & windows에서 저런 결과는 구경하기 힘드네요.
멀티 코어에서 나타나는 버그인가요?
pthread_win32 dll_latest의
pthread_win32 dll_latest의 pthreadVC2.dll과 pthreadVC2.lib를 이용했고,
컴파일러는 VS6.0을 이용하고 있습니다.
멀티코어든 싱글코어든 저는 결과가 이상하게 출력되네요...
혹시 MSVC 때문에 그런건지 모르겠네요...
댓글 달기