Linux中pthread_create传递参数的若干情况

2017-10-12  本文已影响0人  walterzhao

0.背景介绍

1.pthread_create介绍

int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg);
头文件 #include<pthread.h>

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

2.1不传递参数

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string>

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 <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string>

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 <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string>

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 <iostream>
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include <iterator>
#include <vector>

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<std::string > get_iplist()
    {
        return m_iplist;
    }

    void set_iplist(std::vector<std::string> iplist)
    {
        m_iplist = iplist;
    }

private:
    A m_a;
    std::vector<std::string> m_iplist;
};

void* thread_object_argv(void* argv)
{   
    B *b = (B*)argv;

    A a = b->get_a();
    std::vector<std::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<std::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<std::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;
}

总结

上一篇下一篇

猜你喜欢

热点阅读