Transferring multiple parameters in a C language thread

Source: Internet
Author: User

Preface: Create a thread in C language, but need to transfer multiple parameters to the thread, we would naturally think that by passing an array or struct, let's take a look at how to transfer the struct and array when creating the thread.

1#include <stdio.h>2#include <pthread.h>3#include <stdlib.h>4#include <string.h>5 6typedefstructStudent7 {8    intnum;9    Charname[Ten];Ten }info; One  A void*message (void*Arg) - { -Info *p = (info*) arg; theprintf"num:%d name:%s\n",p->num,p->name); -  - } -  + void*read_routine1 (void*Arg) -{int*FD; +FD = (int*) arg; A     //fd[0] = ((int *) arg) [0]; at     //fd[1] = ((int *) arg) [1]; -printf"fd[0]:%d fd[1]:%d\n", fd[0],fd[1]); - } -  -  - intMainintargcChar*argv[]) in {     -Info *st = (info*)malloc(sizeof(info)); toSt->num =Ten; +strcpy (St->name,"xiaoming"); -     intfd[2]; thefd[0] = A; *fd[1] = +; $ pthread_t Tid1,tid2;Panax Notoginseng     /*Create two threads, the first one is a struct, the second is an array*/ -Pthread_create (&tid2,null,message, (void*) st); thePthread_create (&tid1,null,read_routine1, (void*) FD); +      while(1); A      Free(ST); the     return 0; +}

After testing, this method is feasible.

Transferring multiple parameters in a C language 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.