어찌보면 당연한 얘기이긴 한데...예부터 보자
(돌려보진 않고 막 짠 코드)

void* testFunc(void* arg) {

...

}


int main() {

pthread_t p_thread[5];

int status[5];

int arg[5] = { 0, 1, 2, 3, 4 };

for (int i = 0; i < 5; i++) {

int thread_id = pthread_create(&p_thread[i], NULL, testFunc, (void *)(&(arg[i])));

}

for (int i = 0; i < 5; i++) {

pthread_join(p_thread[i], reinterpret_cast<void **>(&status[i]));

}

64bit에서는 요런식으로 pthread_join을 하게 되면 status가 4byte인 포인터를 넘겨주었는데
이대로 실행을 하게 되면 pthread_join()내부에서 8byte의 값으로 덮어쓰게 된다.
따라서 stack corrupted 오류가 발생하거나, 이상한 값이 들어올 수 있당

따라서 status를 8byte인 long long같은 형식으로 정의하면 된당

C++에서 32비트와 64비트를 #ifdef를 사용해서 정의하는 방법을 설명

결국 리눅스와 윈도우를 한꺼번에 가져갈 수는 없구만...

http://stackoverflow.com/questions/1505582/determining-32-vs-64-bit-in-c

+ Recent posts