向 C 中的线程返回多个值时出错
Error when returning multiple values to threads in C
我设置了几个线程,每个线程都会填充多个值,它应该查询数据库和return多个值。
单线程可以显示正确的值,但是当它们 return 值到主函数时出现问题。
主要功能是:
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 是 Codeblocks,与 mingw64 不同,当我用 mingw32 编译它时,我必须在那里添加一个链接器 MinGW\lib\libpthread_s.dll.a
和 MinGW\lib\libpthread.a
。我不知道这是否可能是问题所在?我该如何解决?
使用pthread_join()
需要更加小心:
- 首先,在使用return值之前,您需要检查函数是否成功。否则你的 return 值将不是你所期望的。
- 您还应该为重设值初始化指针。
我return值的方式实际上不是问题。那是我使用的图书馆的问题。
所以我使用 Mingw 安装管理器(应该默认安装)重新安装 min3gw2-libpthread...
库。它奏效了。
我设置了几个线程,每个线程都会填充多个值,它应该查询数据库和return多个值。
单线程可以显示正确的值,但是当它们 return 值到主函数时出现问题。
主要功能是:
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 是 Codeblocks,与 mingw64 不同,当我用 mingw32 编译它时,我必须在那里添加一个链接器 MinGW\lib\libpthread_s.dll.a
和 MinGW\lib\libpthread.a
。我不知道这是否可能是问题所在?我该如何解决?
使用pthread_join()
需要更加小心:
- 首先,在使用return值之前,您需要检查函数是否成功。否则你的 return 值将不是你所期望的。
- 您还应该为重设值初始化指针。
我return值的方式实际上不是问题。那是我使用的图书馆的问题。
所以我使用 Mingw 安装管理器(应该默认安装)重新安装 min3gw2-libpthread...
库。它奏效了。