【发布时间】:2016-07-12 23:01:33
【问题描述】:
我在这里浏览了大量围绕我的问题的答案,但没有一个回答我面临的问题。
我正在用 C 编写一个多线程程序,该程序通过矩阵乘法来评估系统性能。
目前我只是尝试在传入几个变量的同时启动一个线程。
这是我的代码
pthread_t *thread = (pthread_t *) malloc(sizeof(pthread_t));
int a = malloc(sizeof(int));
int b = malloc(sizeof(int));
a = 0;
b = size;
void **args = (void **) malloc(2 * sizeof(void *));
args[0] = (void *)a;
args[1] = (void *)b;
pthread_create(&thread[0], NULL, matrixMath, &args);
还有matrixMath方法:
void* matrixMath(void* args) {
int start = *((int *)args[0]);
int end = *((int *)args[1]);
printf("Start: %d, End: %d\n", start, end);
return NULL;
}
每当我尝试编译时,我都会在“int start”和“int end”声明中得到“无效使用 void 表达式”。我以the discussion found here 为基础。有什么帮助吗?
【问题讨论】:
-
为什么将
a和b设置为malloc返回的值,然后将它们设置为其他值,从而泄漏您分配的内存?
标签: c multithreading pthreads syntax-error void-pointers