pthread_create: allocate (malloc) 将整数作为最后一个参数传递的优点?
pthread_create: advantages of allocate (malloc) for passing an integer as the last argument?
我发现了几个使用动态内存分配方法将整数作为最后一个参数传递的代码,例如这个例子:
void *foo(void *i) {
int a = *((int *) i);
free(i);
}
int main() {
pthread_t thread;
for ( int i = 0; i < 10; ++1 ) {
int *arg = malloc(sizeof(*arg));
if ( arg == NULL ) {
fprintf(stderr, "Couldn't allocate memory for thread arg.\n");
exit(EXIT_FAILURE);
}
*arg = i;
pthread_create(&thread, 0, foo, arg);
}
/* Wait for threads, etc */
return 0;
}
否则,我找到了使用数组方法的学术示例,例如传递结构的示例:
//Runnable example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct my_Struct{
int index;
int value;
};
void* my_Func(void *received_struct){
struct my_Struct *struct_ptr = (struct my_Struct*) received_struct;
printf("index: %d | value: %d \n", struct_ptr->index, struct_ptr->value);
//Now use 'struct_ptr->index', 'struct_ptr->value' as you wish
}
int main(){
struct my_Struct mystruct_1[5];
printf("\nEnter 5 numbers:\n");
for (int i=0; i<5; i++){
scanf("%d", &mystruct_1[i].value);
mystruct_1[i].index = i;
}
pthread_t tid[5];
for(int i=0; i<5; i++){
pthread_create(&(tid[i]), NULL, my_Func, &mystruct_1[i]);
}
for (int i=0; i<5; i++){
pthread_join(tid[i], NULL);
}
}
//To run: gcc [C FILE].c -lpthread -lrt
// ./a.out
//Grepper Profile: https://www.codegrepper.com/app/profile.php?id=9192
动态内存分配方式有什么优势?
更新:我在基于 gcc 的 IBM i 文档中发现他们使用 memory allocation
当你只想传递一个整数时,你可以将它转换为一个指针:
void *foo(void *i) {
int a = (int)i;
}
for ( int i = 0; i < 10; ++1 ) {
pthread_create(&thread, 0, foo, (void*)i);
}
这提供了最大的效率并且没有寿命问题。
pthread_create()
的最后一个参数在新建线程的栈上传递。当它是一个整数或任何其他大小小于或等于指针大小的东西时,可以通过强制转换将参数按原样传递给 pthread_create()
。但这不是推荐的制作方法,因为它不是 portable 在机器上,例如整数与指针的大小不同。让我们编译以下简单程序:
#include <pthread.h>
#include <stdio.h>
void* thread_task(void* arg)
{
printf("Thread received parameter: %d\n", (int)arg);
return NULL;
}
int main(void)
{
pthread_t tid;
int i = 45;
pthread_create(&tid, NULL, thread_task, (void *)i);
pthread_join(tid, NULL);
return 0;
}
gcc 编译器发出以下警告:
$ gcc pcreate.c -o pcreate -lpthread
pcreate.c: In function ‘thread_task’:
pcreate.c:6:45: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
printf("Thread received parameter: %d\n", (int)arg);
^
pcreate.c: In function ‘main’:
pcreate.c:15:43: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pthread_create(&tid, NULL, thread_task, (void *)i);
因此,在结构或 table 条目或动态分配的内存区域上使用指针是使其成为 portable 的方法。前面的程序可以固定为:
#include <pthread.h>
#include <stdio.h>
void* thread_task(void* arg)
{
printf("Thread received parameter: %d\n", *((int *)arg));
return NULL;
}
int main(void)
{
pthread_t tid;
int t[1] = { 45 };
pthread_create(&tid, NULL, thread_task, (void *)t);
pthread_join(tid, NULL);
return 0;
}
编译器不再发出任何警告:
$ gcc pcreate.c -o pcreate -lpthread
$ ./pcreate
Thread received parameter: 45
我发现了几个使用动态内存分配方法将整数作为最后一个参数传递的代码,例如这个例子:
void *foo(void *i) {
int a = *((int *) i);
free(i);
}
int main() {
pthread_t thread;
for ( int i = 0; i < 10; ++1 ) {
int *arg = malloc(sizeof(*arg));
if ( arg == NULL ) {
fprintf(stderr, "Couldn't allocate memory for thread arg.\n");
exit(EXIT_FAILURE);
}
*arg = i;
pthread_create(&thread, 0, foo, arg);
}
/* Wait for threads, etc */
return 0;
}
否则,我找到了使用数组方法的学术示例,例如传递结构的示例:
//Runnable example
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
struct my_Struct{
int index;
int value;
};
void* my_Func(void *received_struct){
struct my_Struct *struct_ptr = (struct my_Struct*) received_struct;
printf("index: %d | value: %d \n", struct_ptr->index, struct_ptr->value);
//Now use 'struct_ptr->index', 'struct_ptr->value' as you wish
}
int main(){
struct my_Struct mystruct_1[5];
printf("\nEnter 5 numbers:\n");
for (int i=0; i<5; i++){
scanf("%d", &mystruct_1[i].value);
mystruct_1[i].index = i;
}
pthread_t tid[5];
for(int i=0; i<5; i++){
pthread_create(&(tid[i]), NULL, my_Func, &mystruct_1[i]);
}
for (int i=0; i<5; i++){
pthread_join(tid[i], NULL);
}
}
//To run: gcc [C FILE].c -lpthread -lrt
// ./a.out
//Grepper Profile: https://www.codegrepper.com/app/profile.php?id=9192
动态内存分配方式有什么优势?
更新:我在基于 gcc 的 IBM i 文档中发现他们使用 memory allocation
当你只想传递一个整数时,你可以将它转换为一个指针:
void *foo(void *i) {
int a = (int)i;
}
for ( int i = 0; i < 10; ++1 ) {
pthread_create(&thread, 0, foo, (void*)i);
}
这提供了最大的效率并且没有寿命问题。
pthread_create()
的最后一个参数在新建线程的栈上传递。当它是一个整数或任何其他大小小于或等于指针大小的东西时,可以通过强制转换将参数按原样传递给 pthread_create()
。但这不是推荐的制作方法,因为它不是 portable 在机器上,例如整数与指针的大小不同。让我们编译以下简单程序:
#include <pthread.h>
#include <stdio.h>
void* thread_task(void* arg)
{
printf("Thread received parameter: %d\n", (int)arg);
return NULL;
}
int main(void)
{
pthread_t tid;
int i = 45;
pthread_create(&tid, NULL, thread_task, (void *)i);
pthread_join(tid, NULL);
return 0;
}
gcc 编译器发出以下警告:
$ gcc pcreate.c -o pcreate -lpthread
pcreate.c: In function ‘thread_task’:
pcreate.c:6:45: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
printf("Thread received parameter: %d\n", (int)arg);
^
pcreate.c: In function ‘main’:
pcreate.c:15:43: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
pthread_create(&tid, NULL, thread_task, (void *)i);
因此,在结构或 table 条目或动态分配的内存区域上使用指针是使其成为 portable 的方法。前面的程序可以固定为:
#include <pthread.h>
#include <stdio.h>
void* thread_task(void* arg)
{
printf("Thread received parameter: %d\n", *((int *)arg));
return NULL;
}
int main(void)
{
pthread_t tid;
int t[1] = { 45 };
pthread_create(&tid, NULL, thread_task, (void *)t);
pthread_join(tid, NULL);
return 0;
}
编译器不再发出任何警告:
$ gcc pcreate.c -o pcreate -lpthread
$ ./pcreate
Thread received parameter: 45