您的位置:首页 > 其它

pthread_create如何传递多个参数

2012-06-19 08:44 267 查看
http://zhidao.baidu.com/question/315398992.html

涉及多参数传递给线程的,都需要使用结构体将参数封装后,将结构体指针传给线程

定义一个结构体

struct mypara

{

var para1;//参数1

var para2;//参数2

}

将这个结构体指针,作为void *形参的实际参数传递

struct mypara pstru;

pthread_create(&ntid, NULL, thr_fn,& (pstru));

函数中需要定义一个mypara类型的结构指针来引用这个参数

void *thr_fn(void *arg)

{

mypara *pstru;

pstru = (* struct mypara) arg;

pstru->para1;//参数1

pstru->para2;//参数2

}

pthread_create函数接受的参数只有一个void *型的指针,这就意味着你只能通过结构体封装超过一个以上的参数作为一个整体传递。这是pthread_create函数的接口限定的,别人已经明确表明我只接受一个参数,你硬要塞给他两个肯定会出错了。所以通过结构体这种组合结构变通一下,同样实现了只通过一个参数传递,但通过结构指针对结构数据成员的引用实现多参数的传递

这种用结构体封装多参数的用法不仅仅用在pthread_create函数中,如果你自己设计的函数需要的参数很多〉=5个以上,都可以考虑使用结构体封装,这样对外你的接口很简洁清晰,你的函数的消费者使用起来也很方便,只需要对结构体各个成员赋值即可,避免了参数很多时漏传、误传(参数串位)的问题

结构体内包含结构体完全没有问题,很多应用都这么使用

举例如下:
http://wenku.baidu.com/view/48a302ed6294dd88d0d26b73.html
#include<stdio.h>

#include<stdlib.h>

#include<pthread.h>

#include<errno.h>

#include<unistd.h>



typedef void* (*fun)(void*);




static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static pthread_cond_t recv_over = PTHREAD_COND_INITIALIZER;

static pthread_cond_t decode_over = PTHREAD_COND_INITIALIZER;

static pthread_cond_t play_over = PTHREAD_COND_INITIALIZER;



void* receive(void*);

void* decode(void*);

void* play(void*);



pthread_t tdec, tplay, trecv;



struct mypara

{

int thread_id;

char *thread_name;

};



int main(int argc, char** argv)

{

struct mypara para;

para.thread_id = 1;

para.thread_name = "recv";



int t1 = 0, t2 = 0, t3 = 0;

t1 = pthread_create(&trecv, NULL, receive,& (para));

if(t1 != 0)

printf("Create thread receive error!\n");



t2 = pthread_create(&tdec, NULL, decode, NULL);

if(t2 != 0)

printf("Create thread decode error!\n");



t3 = pthread_create(&tplay, NULL, play, NULL);

if(t3 != 0)

printf("Create thread play error!\n");



pthread_join(trecv, NULL);

pthread_join(tdec, NULL);

pthread_join(tplay, NULL);

printf("leave main\n");

exit(0);

}



void* receive(void* arg)

{

printf("Start receive\n");

int i = 0;

char *s = NULL;



struct mypara *recv_para;

recv_para = (struct mypara *)arg;

i = (*recv_para).thread_id;

s = (*recv_para).thread_name;

printf("NO : %d Name : %s\n",i,s);



sleep(2);

pthread_mutex_lock(&mutex);

while (1)

{

printf("Receiving...\n");

sleep(1);

pthread_cond_signal(&recv_over);

pthread_cond_wait(&decode_over, &mutex);

}

printf("End receive\n");

pthread_exit(0);

}



void* decode(void* arg)

{

printf("Start decode\n");



while (1)

{

pthread_cond_wait(&recv_over, &mutex);

printf("Decoding...\n");

sleep(1);

pthread_cond_broadcast(&decode_over); //inform player ready to play

}

printf("End decode\n");

pthread_exit(0);

}





void* play(void* arg)

{

int ret;

printf("Start play\n");



while(1)

{

pthread_cond_wait(&decode_over, &mutex); //wait the signal from decoder

printf("Playing...\n");

sleep(1);

}

pthread_mutex_unlock(&mutex);

printf("End play\n");

pthread_exit(0);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: