linux pthread创建线程传参,Linux中pthread_create传递参数的若干情况

0.背景介绍

今天在工作中要使用pthread_create函数开启一个线程,并且需要传递参数,其中遇到了一些问题,现在来总结一下。

1.pthread_create介绍

函数原型:

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

第一个参数为指向线程标识符的指针(例如:pthread_t p_thread)

第二个参数用来设置线程属性

第三个参数是线程运行函数的起始地址

第四个参数是运行函数的参数

在Linux系统中如果希望开启一个新的线程,可以使用pthread_create函数,它实际的功能是确定调用该线程函数的入口点,在线程创建以后,就开始运行相关的线程函数。

头文件 #include

pthread_create的返回值表征进程是否创建成功。其中0:成功,-1:失败。

编译的时候,需要添加编译条件 -pthread。例如:g++ pthread_test.cpp -o pthread_test -pthread

2.pthread_create开启线程,传递参数的几种情况

2.1不传递参数

#include

#include

#include

#include

#include

using namespace std;

pthread_t pid;

void* thread_no_argv(void* argv)

{

while(1)

{

cout << "i am new thread"<< endl;

sleep(1);

}

}

int main()

{

pthread_create(&pid,NULL,thread_no_argv,NULL);

pthread_detach(pid);//回收线程资源

while(1)

{

}

return 0;

}

2.2 传递一个简单类型

#include

#include

#include

#include

#include

using namespace std;

pthread_t pid;

void* thread_int_argv(void* argv)

{

int i = *(int*)argv;//进行类型装换,具体的类型具体转换

while(1)

{

cout << "i am new thread"<< endl;

cout << "i:" << i << endl;

sleep(1);

}

}

int main()

{

int i = 10;

pthread_create(&pid,NULL,thread_int_argv,(void*)&i);//最后一个参数为void*类型,传递变量的地址,其实其他的类型也遵循这个原则

pthread_detach(pid);

while(1)

{

}

return 0;

}

2.3传递一个结构体对象

#include

#include

#include

#include

#include

using namespace std;

pthread_t pid;

typedef struct

{

int i;

std::string str;

}my_adt;

void* thread_struct_argv(void* argv)

{

my_adt* adt = (my_adt*)argv;

while(1)

{

cout << "i am new thread"<< endl;

cout << "i:" << adt->i << endl;

cout << "str:" << adt->str << endl;

sleep(1);

}

}

int main()

{

my_adt adt;

adt.i = 22;

adt.str = "walter";

pthread_create(&pid,NULL,thread_struct_argv,(void*)&adt);

pthread_detach(pid);

while(1)

{

}

return 0;

}

2.4传递一个类对象

#include

#include

#include

#include

#include

#include

#include

using namespace std;

pthread_t pid;

class A

{

public:

int get_i()

{

return m_i;

}

void set_i(int i)

{

m_i = i;

}

std::string get_str()

{

return m_str;

}

void set_str(std::string str)

{

m_str = str;

}

private:

int m_i;

std::string m_str;

};

class B

{

public:

A get_a()

{

return m_a;

}

void set_a(A a)

{

m_a = a;

}

std::vector<:string> get_iplist()

{

return m_iplist;

}

void set_iplist(std::vector<:string> iplist)

{

m_iplist = iplist;

}

private:

A m_a;

std::vector<:string> m_iplist;

};

void* thread_object_argv(void* argv)

{

B *b = (B*)argv;

A a = b->get_a();

std::vector<:string> iplist = b->get_iplist();

cout << "a.i:" << a.get_i() << endl;

cout << "a.str:" << a.get_str() << endl;

cout << "iplist.size:" << iplist.size();

for(std::vector<:string>::iterator iter = iplist.begin();iter != iplist.end();iter++)

{

cout << "iplist:" << std::string(*iter) << endl;

}

while(1)

{

sleep(1);

}

}

int main()

{

A a;

a.set_i(99);

a.set_str("zx");

std::vector<:string> iplist;

iplist.push_back("1.1.1.1");

iplist.push_back("2.2.2.2");

B *b = new B();//注释1

b->set_a(a);

b->set_iplist(iplist);

pthread_create(&pid,NULL,thread_object_argv,(void*)b);

pthread_detach(pid);

while(1)

{

}

return 0;

}

注释1:这个地方最好使用new一个对象的方式,这样可以为复杂的数据结构分配内存,否则的话就可能会出现内存分配的异常:std::bad_alloc。

总结

上面介绍了几种常用的传递参数的情况,在实际的情况中灵活变通使用。

如果上面介绍的情况有什么问题,还请大家指出了,或者一起讨论,或者还有什么更好的方法,也希望和我交流。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值