Pthread_create () passing multiple parameters when creating a thread

Source: Internet
Author: User

Because the interface defines only one entry void *arg

int pthread_create (pthread_t *tidp,const pthread_attr_t *attr, (void*) (*START_RTN) (void*), void *arg);


Therefore, if you want to pass parameters, you need to encapsulate the structure and pass multiple parameters to the thread through a struct.

typedef struct{    FUNCPTR entry;/* Function entry *    /void *arg[10];/* parameter */}func;void *start (void *arg) {    FUNC *func = NULL ;    (* (Func->entry)) (Func->arg[0], func->arg[1], func->arg[2], func->arg[3], func->arg[4],            func->arg[5], func- >ARG[6], func->arg[7], func->arg[8], func->arg[9]);    return NULL;} int pthread_ctreate (pthread_t *tidp,const pthread_attr_t *attr, void * (*FUNCPTR) (void *, void *, void *), unsigned args,. ..) {/    * struct Body of constructor collection */    FUNC function = {0};    Function.entry = funcptr;    for (i = 0; i < args; i++)    {        Func->arg[i] = Arg[i];    }     Set thread properties (slightly)    pthread_create (NULL, &attr, start, (void *) &function);    

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Pthread_create () pass in more than one parameter when creating a thread

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.