关于向线程的运行函数传递一个参数和多个参数

1.线程创建

#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号

参数说明:
第一个参数为指向线程标识符的指针。
  第二个参数用来设置线程属性。
  第三个参数是线程运行函数的起始地址。
  最后一个参数是运行函数的参数。
  另外,在编译时注意加上-lpthread参数,以调用静态链接库。因为pthread并非Linux系统的默认库

2.向运行函数只传递一个参数时:直接定义一个变量,将该变量的地址作为arg参数传入。

#include<stdio.h>
#include <pthread.h>
 // int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
void *func(void *arg)
{
        static char *ret="lanlan!!!";

        printf("t1: param=%d\n",*((int *)arg));
        printf("t1: func's id =%ld\n",(unsigned long)pthread_self());

        pthread_exit((void *)ret);
}

int main()
{
        pthread_t t1;
        int param = 10; //只传递一个参数
        char *pret = NULL;
        int ret = pthread_create(&t1,NULL,func,(void *)&param);//注意取地址符号
        
        if(ret == 0){
                printf("main:  pthread_create success\n");
                printf("main:  id=%ld\n",(unsigned long)pthread_self());
        }
        pthread_join(t1,(void **)&pret); //指针pret指向的是线程执行函数static char *ret的地址

        printf("main: return data=%s\n",pret);
        return 0;
}

2.向执行函数传递多个参数时:如果需要向执行函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg参数传入。

#include<stdio.h>
#include <pthread.h>
#include<string.h>
#include<stdlib.h>

 // int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);

struct text
{
        int a;
        char b;
        char c[128];
        char *d;
};

void *func(void *arg)
{
        static char *ret="lanlan!!!";

        struct text *arg1 = (struct text *)arg;//!!!!!!!!!!!!!

        printf("t1: a=%d\n",arg1->a);
        printf("t1: b=%c\n",arg1->b);
        printf("t1; c=%s\n",arg1->c);
        printf("t1: d=%s\n",arg1->d);

        printf("t1: func's id =%ld\n",(unsigned long)pthread_self());

        pthread_exit((void *)ret);
}
int main()
{
        pthread_t t1;
        char *pret = NULL;

//      struct text param = {1056,'L',"SADSDAD","dada"};
/*      struct text param = {1056,'L'};
        memset(param.c,0,sizeof(param.c));
        strcpy(param.c,"SADSDAD");
        
        param.d = (char *)malloc(128);
        strcpy(param.d,"dada");
  */
  //另一种写法
        struct text param = {
                .a = 1056,
                .b = 'l',
                .c = "adsad",
                .d = "lanlan",
        };

        int ret = pthread_create(&t1,NULL,func,(void *)&param);

        if(ret == 0){
                printf("main:  pthread_create success\n");
                printf("main:  id=%ld\n",(unsigned long)pthread_self());
        }

        pthread_join(t1,(void **)&pret);
        printf("main: return data=%s\n",pret);
//        free(param.d);//malloc堆区的内存释放,防止内存泄漏
        return 0;
}
  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,线程函数通常只接受一个参数,即void类型指针。然而,我们可以通过使用结构体或者指针的方式,传递多个参数线程函数。 首先,我们可以创建一个结构体来存储多个参数,然后将结构体传递线程函数。例如: ```c #include <stdio.h> #include <pthread.h> struct ThreadArgs { int arg1; float arg2; char arg3; }; void *threadFunc(void *args) { struct ThreadArgs *myArgs = (struct ThreadArgs *)args; printf("arg1: %d\n", myArgs->arg1); printf("arg2: %f\n", myArgs->arg2); printf("arg3: %c\n", myArgs->arg3); pthread_exit(NULL); } int main() { pthread_t thread; struct ThreadArgs args = {10, 3.14, 'A'}; pthread_create(&thread, NULL, threadFunc, (void *)&args); pthread_join(thread, NULL); return 0; } ``` 上述代码中,我们定义了一个结构体ThreadArgs,包含了三个不同类型的参数。然后,我们在主函数中创建了一个ThreadArgs类型的变量args并对其进行初始化。接着,我们通过pthread_create函数创建了一个新的线程,并将args结构体的地址作为参数传递线程函数threadFunc。 在线程函数中,我们将传递进来的参数强制转换为ThreadArgs类型指针,并访问各个参数的值,然后打印输出。 这样,我们就成功地将多个参数传递线程函数了。 另外一种方式是将参数存储在数组或者指针中,并将该数组或指针传递线程函数。在线程函数内部,我们可以通过解引用指针或索引数组的方式来使用这些参数。 总之,通过使用结构体或者指针,我们可以很容易地传递多个参数线程函数,并在函数内部使用这些参数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值