Pthread_create Pass Parameters

Source: Internet
Author: User


#include <iostream> #include <pthread.h>

using namespace Std;

pthread_t thread;

void *fn (void *arg)
{
int i = * (int *) arg;
cout<< "i =" <<i<<endl;

return ((void *) 0);
}

int main ()
{
int err1;
int i=10;

ERR1 = Pthread_create (&thread, NULL, &FN, &i);
Pthread_join (thread, NULL);

}

————————————————————————————————

Thread creation function:
int Pthread_create (pthread_t *tid, const pthread_attr_t *attr, void * (*FUNC) (void *), void *arg);
The parameter func represents one parameter void *, and the return value is void *;
For void *arg, parameters are passed in, and in the case of GCC 3.2.2, the following two ways can be passed in.
int ssock;
int tcpechod (int fd);
1.pthread_create (&th, &ta, (void * (*) (void *)) Tcpechod, (void *) ssock);
2.pthread_create (&th, &ta, (void * (*) (void *)) &tcpechod, (void *) &ssock);


This article from the Chinaunix blog, if you view the original please point:http://blog.chinaunix.net/u/9643/showart_49987.html

Pthread_create (&tid,&attr,&func, (void) arg) can only pass one argument to the Func, if you want to pass more than one parameter. define a structure and pass this structure .

can pass multiple parameters to the thread_function when Pthread_create. Code:typedef Union {
size_t arg_cnt;
Any_possible_arg_types;
} Arg_type;

Arg_type Args[arg_num + 1];
args[0].arg_cnt = Arg_num;
Args[1].xxx = ...;

Pthread_create (..., thread_function, &args[0]);

Go inside and parse it yourself.

------------------------- The use of pthread_create transfer parameters

Recently, the program that started to write the socket again. The
has a fork mode other than the previous use of Select or the earliest heavy-weight.
this time using pthread to handle the request received on the server side.
However, there is a problem, how to pass parameters to thread's handler
Man pthread_create can see only 4th argument can be applied.
How to use it. Looking for the previous sample code, the original, do casting can. The

string is not a problem, and if it is an integer, write it.

void Pfunc (void *data)
{
int i = (int) data;
...
}

Main ()
{
int ival=100;
pthread_t th;
...
Pthread_create (&th, NULL, Pfunc, (void *) ival);
}

If you encounter more than one number of parameters. Just wrap it up in struct, pointer it over.

struct test
{
int no;
Char name[80];
};

void Pfunc (void *data)
{
struct Test tt = (struct test*) data
...
}

Main ()
{
struct test itest
pthread_t th;
...
Itest.no=100;
strcpy (Itest.name, "Hello");
...
Pthread_create (&th, NULL, Pfunc, (void *) & itest);
..
}

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.