转自:http://blog.csdn.net/yeyuangen/article/details/6757525

#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);

}

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

线程创建函数:
int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void * (*func)(void *), void *arg);
参数func 表示代一个参数void *,返回值也为void *;
对于void *arg,参数传入,在gcc 3.2.2条件下,以下面两种方式传入都可编译通过。
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);

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/9643/showart_49987.html

pthread_create(&tid,&attr,&func,(void)arg)只能传递一个参数给func,要是要传一个以上的参数呢?请指教

定义一个结构然后传这个结构 

pthread_create时,能否向thread_function传递多个参数?

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]);

进去了自己解析。。

-------------------------

pthread_create 傳遞參數的用法

 

最近,又開始寫 socket 的程式. 
有別於以前用 select 或最早的 heavy-weight 的 fork 方式. 
這次改用 pthread 來 handle server 端所收到的 request . 
不過, 有一個問題, 如何傳參數給 thread 的 handler 
man pthread_create 可以看到只有 4th argument 可以應用. 
至於怎麼用. 找了以前的 sample code, 原來,做 casting 就可以.

string 沒問題, 如果是傳 integer 就這樣寫..

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

main()
{
int ival=100;
pthread_t th;
...
pthread_create( &th, NULL, pfunc, (void *) ival );
}

如遇到多個參數. 就包成 struct , 傳 pointer 過去吧 ~

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 );
..
}

pthread_create传递参数的更多相关文章

  1. Linux线程体传递参数的方法详解

    传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thre ...

  2. Vue 给子组件传递参数

    Vue 给子组件传递参数 首先看个例子吧 原文 html <div class="container" id="app"> <div clas ...

  3. [转] C++的引用传递、指针传递参数在java中的相应处理方法

    原文出处:[http://blog.csdn.net/conowen/article/details/7420533] 首先要明白一点,java是没有指针这个概念的. 但是要实现C++的引用传递.指针 ...

  4. 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)

    目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...

  5. 在 Angularjs 中 ui-sref 和 $state.go 如何传递参数

    1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...

  6. 【hadoop】如何向map和reduce脚本传递参数,加载文件和目录

    本文主要讲解三个问题:       1 使用Java编写MapReduce程序时,如何向map.reduce函数传递参数.       2 使用Streaming编写MapReduce程序(C/C++ ...

  7. python 函数传递参数的多种方法

    python中函数根据是否有返回值可以分为四种:无参数无返回值,无参数有返回值,有参数无返回值,有参数有返回值. Python中函数传递参数的形式主要有以下五种,分别为位置传递,关键字传递,默认值传递 ...

  8. Apache AB 如何传递参数

    AB使用时,网上通篇一律,在进行示例时使用的连接一般都是http://*.com,这种写法是没有带参数,如果你想测试一个写入的Case,那需要传递参数给后台,如何传递参数呢? 这里有一个登录的请求,需 ...

  9. js跳转传递参数

    额,利用j获取了GridView中选中行数据后,通过JavaScript做跳转,传递参数的时候发现,当参数有中文的时候就会乱码, 当然出现这种情况的时候就需要对跳转的url进行编码 var urlX ...

随机推荐

  1. poll函数

    poll函数与select函数的功能基本一样,其定义如下: #include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int ...

  2. Selenium FF WebDriver运行时开启firebug的2种方式

    上一次我实测FF webdriver 加载firefoxhttp://www.cnblogs.com/tobecrazy/p/3997375.html 那么问题就来了,既然能加载firebug能否在运 ...

  3. Java for LeetCode 223 Rectangle Area

    Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined b ...

  4. HTML5 video 支持air play

    < video src="/path/to/video.mp4" x-webkit-airplay="allow" preload controls> ...

  5. Effective C++ -----条款53:不要轻忽编译期的警告

    严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取“无任何警告”的荣誉. 不要过度依赖编译器的报警能力,因为不同的编译器对待事情的态度并不相同.一旦移植到另一个编译器上,你元 ...

  6. 【python】入门学习(九)

    面向对象编程 class 定义类,类的值可以修改 _ _init_ _(self) 初始化函数,创建类时自动调用 self 指向对象本身,可以用其他的名字 但不建议 #person.py class ...

  7. UISrollView

    1.  contentOffset 默认CGPointZero,用来设置scrollView的滚动偏移量. // 设置scrollView的滚动偏移量 scrollView.contentOffset ...

  8. ios layer 动画-(transform.rotation篇)

    x轴旋转: CABasicAnimation *theAnimation; theAnimation=[CABasicAnimation animationWithKeyPath:@"tra ...

  9. Sightseeing(poj 3463)

    题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...

  10. 搞笑世界杯(codevs 1060)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...