函数“pthread_create”的参数太多
Too many arguments to function ‘pthread_create’
如标题所示,我正在尝试弄清楚如何将我所有的启动例程函数参数传递到我的 pthread 中,但我的运气并不好
pthread_create(firstThread,NULL,sort,first,first_size);
我正在尝试传递带有两个参数、数组指针和数组大小的排序函数,但我不熟悉将多个参数传递到线程的语法,我该如何实现?
pthread_create
将单个参数传递给线程函数,并且该参数必须是指针。
只有两种主要方法可以将某些东西传递给线程函数:
- 传递适合
void*
的对象,然后进行转换;
- 或者,在堆上创建一个对象(使用
malloc
),然后将这个新创建的对象的地址传递给线程函数。在这种情况下,您必须确保线程函数 free
在使用参数后占用内存。
- 先前选项的一种变体是将指针传递给全局可用的对象(例如,全局变量),但这通常不是可接受的选项。
- 另一种变体是将地址传递给调用函数主体中本地定义的对象,但这在学校示例之外很少有用,因为不可能以这种方式实现并行执行(线程将有在对象离开调用范围之前加入。
在您的情况下,它可能如下所示:
typedef struct {
int* arrayPtr;
size_t arraySize;
} ThreadedSortArgs;
void threadedSort(void* voidArg) {
ThreadedSortArgs* arg = voidArg;
// Work with args from arg->arrayPtr, arg->arraySize
free(arg);
}
// call it somewhere
ThreadedSortArgs* args = malloc(sizeof(ThreadedSortArgs));
pthread_create(firstThread, NULL, threadedSort, args);
// ...
在 pthread_create
中使用多个参数
您必须在结构中设置所有参数,请参见下面的工作示例:
pthread_arg.c
#include <stdio.h>
#include <pthread.h>
#define _DEBUG 1
typedef struct arg {
int *array;
int len;
} t_arg;
void *sort(void *ptr);
int main(void) {
pthread_t firstThread;
int first[5] = {3,5,19,6,1};
int first_size = 5;
t_arg arg = {.array = first, .len = first_size}; // assign your variable to the variable arg which is a t_arg struct
pthread_create( &firstThread, NULL, &sort, &arg); // give the struct address to pthread_create
pthread_join(firstThread, NULL);
}
void *sort(void *ptr) {
t_arg *arg = ptr; // assign the ptr to arg;
int n = arg->len; // now you can work with your function
int *a = arg->array;
int i, tmp, j;
for (i = 1; i < n; i++) {
tmp = a[i];
j = i - 1;
while (j >= 0 && a[j] > tmp) {
a[j + 1] = a[j];
--j;
}
a[j + 1] = tmp;
}
#ifdef _DEBUG
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
putchar('\n');
#endif
return (void *)1; // to compile with -Wall -Wextra -Werror
}
编译
gcc -O3 pthread_arg.c -lpthread && ./a.out
编辑:您必须更改
void sort(void *arg)
至
void *sort2(void *arg)
因为返回 void 指针的函数不同于 void 函数。
并删除 struct sorting *s = (struct sorting)arg;
=> struct sorting *s = arg;
的强制转换,没有必要。
如标题所示,我正在尝试弄清楚如何将我所有的启动例程函数参数传递到我的 pthread 中,但我的运气并不好
pthread_create(firstThread,NULL,sort,first,first_size);
我正在尝试传递带有两个参数、数组指针和数组大小的排序函数,但我不熟悉将多个参数传递到线程的语法,我该如何实现?
pthread_create
将单个参数传递给线程函数,并且该参数必须是指针。
只有两种主要方法可以将某些东西传递给线程函数:
- 传递适合
void*
的对象,然后进行转换; - 或者,在堆上创建一个对象(使用
malloc
),然后将这个新创建的对象的地址传递给线程函数。在这种情况下,您必须确保线程函数free
在使用参数后占用内存。 - 先前选项的一种变体是将指针传递给全局可用的对象(例如,全局变量),但这通常不是可接受的选项。
- 另一种变体是将地址传递给调用函数主体中本地定义的对象,但这在学校示例之外很少有用,因为不可能以这种方式实现并行执行(线程将有在对象离开调用范围之前加入。
在您的情况下,它可能如下所示:
typedef struct {
int* arrayPtr;
size_t arraySize;
} ThreadedSortArgs;
void threadedSort(void* voidArg) {
ThreadedSortArgs* arg = voidArg;
// Work with args from arg->arrayPtr, arg->arraySize
free(arg);
}
// call it somewhere
ThreadedSortArgs* args = malloc(sizeof(ThreadedSortArgs));
pthread_create(firstThread, NULL, threadedSort, args);
// ...
在 pthread_create
中使用多个参数您必须在结构中设置所有参数,请参见下面的工作示例:
pthread_arg.c
#include <stdio.h>
#include <pthread.h>
#define _DEBUG 1
typedef struct arg {
int *array;
int len;
} t_arg;
void *sort(void *ptr);
int main(void) {
pthread_t firstThread;
int first[5] = {3,5,19,6,1};
int first_size = 5;
t_arg arg = {.array = first, .len = first_size}; // assign your variable to the variable arg which is a t_arg struct
pthread_create( &firstThread, NULL, &sort, &arg); // give the struct address to pthread_create
pthread_join(firstThread, NULL);
}
void *sort(void *ptr) {
t_arg *arg = ptr; // assign the ptr to arg;
int n = arg->len; // now you can work with your function
int *a = arg->array;
int i, tmp, j;
for (i = 1; i < n; i++) {
tmp = a[i];
j = i - 1;
while (j >= 0 && a[j] > tmp) {
a[j + 1] = a[j];
--j;
}
a[j + 1] = tmp;
}
#ifdef _DEBUG
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
putchar('\n');
#endif
return (void *)1; // to compile with -Wall -Wextra -Werror
}
编译
gcc -O3 pthread_arg.c -lpthread && ./a.out
编辑:您必须更改
void sort(void *arg)
至
void *sort2(void *arg)
因为返回 void 指针的函数不同于 void 函数。
并删除 struct sorting *s = (struct sorting)arg;
=> struct sorting *s = arg;
的强制转换,没有必要。