여러 개의 스레드를 설정했는데 각 스레드는 여러 값으로 채워지고 DB에 쿼리를 만들고 여러 값을 반환해야합니다.
단일 스레드는 올바른 값을 표시 할 수 있지만 주 함수에 값을 반환 할 때 문제가 발생했습니다.
주요 기능은 다음과 같습니다.
for(int i=0; i<n; i++){
struct thread_args* args = malloc(sizeof(*args));
args->str = str;
args->tri_input = tri_input;
args->length = length;
res = pthread_create(&t_id[i], NULL, threads, args);
if(res != 0)
printf("Failed to create %d th thread.\n", i);
}
for(int i=0; i<n; i++){
void *returnRes;
pthread_join(t_id[i], &returnRes);
indicesArr[i] = returnRes;
printf("in main thread, indicesArr[%d].length: %d\n", i, indicesArr[i] -> length);
}
단일 스레드 :
void *threads(void *args_){
struct thread_args *args = (struct thread_args *)args_;
char* str = args -> str;
struct IntArrLenArr* indicesArr = NULL;
indicesArr = malloc(sizeof(*indicesArr));
int length = args -> length;
PGconn *dbconn = DBconnect(str);
*indicesArr = duncitonReturnIndicesArr(dbconn);
printf("in threads, indicesArr -> length: %d\n", indicesArr->length);
free(args);
PQfinish(dbconn);
return indicesArr;
}
결과는 다음과 같습니다.
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 0
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 0
in main threads, indicesArr[0].length: -1949512500
in threads, indicesArr -> length: 2
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 4
in threads, indicesArr -> length: 1
in threads, indicesArr -> length: 4
Process returned -1073741819 (0xC0000005) execution time : 6.551 s
Press any key to continue.
win10에서 mingw64로 컴파일하면 잘 작동하는 것 같지만 어떻게 든 32 비트로 컴파일해야합니다. 내 IDE는 mingw64 다르다, 나는 링커를 추가했다, Codeblocks입니다 MinGW\lib\libpthread_s.dll.a
그리고 MinGW\lib\libpthread.a
나는 mingw32로가 컴파일 할 때. 문제가 될 수 있는지 모르겠어요? 어떻게 고칠 수 있습니까?
값을 반환하는 방식은 실제로 문제가되지 않았습니다. 내가 사용한 도서관의 문제였습니다. 그래서 Mingw 설치 관리자 (기본적으로 설치되어야 함)를 사용하여 min3gw2-libpthread...
라이브러리 를 다시 설치합니다 . 그리고 그것은 작동했습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다