pthread.h 相关函数使用方法集锦之线程操作

前言

pthread(POSIX thread),简称为pthread,是线程的POSIX标准,在类Unix操作系统中(Unix、Linux、Mac OS X等),都是用pthread作为操作系统的线程。<pthread.h>作为其编程标准的头文件,本文探讨里面的常用函数意义以及使用方法。

线程

pthread_create

pthread_create是UNIX环境创建线程函数 。共四个参数:

  • 第一个参数为指向线程标识符的指针,type: pthread_t*
  • 第二个参数用来设置线程属性
  • 第三个参数是线程运行函数的起始地址, type: (void*)(*)(void*)
  • 第四个参数是运行函数的参数,type: void *

无参数函数

pthread_create创建线程执行无参数函数的demo。

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

using namespace std;

// 注意函数定义: void* (*)(void*)
void* test(void *ptr){
    cout << "hello world." << endl;
}

int main()
{
    pthread_t tid; // 声明线程id typedef unsigned
    pthread_create(&tid, NULL, test, NULL); // 创建线程
    pthread_join(tid, NULL);
}

带参数函数

pthread_create只支持单个指针,只能传一个参数;如果要传多个参数,封装成结构体形式。

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

using namespace std;

struct Param{
    string name;
    int age;
};

void* param_test(void* p){
    Param *param = (Param*)p;
    cout << "hello " << param->name << endl;
    cout << "age = " << param->age << endl;
}

int main()
{
    pthread_t tid;
    Param *ptr = new Param();
    ptr->name = "pthread";
    ptr->age = 20;
    pthread_create(&tid, NULL, param_test, ptr);
    pthread_join(tid, NULL);
}

类成员函数

调用类成员函数的方式有点复杂,需要另外一个函数封装,函数参数显式传入类实例指针,之后便与调用带参数函数的方法类似。

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

using namespace std;

class PthreadClass{
    public:
        void say_hello(){
            cout << "hello world" << endl;
        }
};

// 封装 传入类实例指针
void* class_test(void *p){
    PthreadClass *ptr = (PthreadClass*)p;
    // 实例调用
    ptr->say_hello();
}

int main()
{
    PthreadClass *ptr = new PthreadClass();
    pthread_t tid;
    pthread_create(&tid, NULL, class_test, ptr);
    pthread_join(tid, NULL);
}

pthread_join

pthread_join用来等待一个线程的结束,线程间同步的操作 ,共两个参数:

  • 第一个参数为线程标识符,即线程ID,type: pthread_t
  • 第二个参数retval为用户定义的指针,用来存储线程的返回值,type: void**
#include <pthread.h>
#include <iostream>

using namespace std;

class PthreadClass{
    public:
        void say_hello(){
            cout << "hello world" << endl;
        }
};

int retval = -1;
void* class_test(void *p){
    PthreadClass *ptr = (PthreadClass*)p;
    ptr->say_hello();
    retval = 0;
    return (void*)&retval;
}

int main()
{
    PthreadClass *ptr = new PthreadClass();
    pthread_t tid;
    pthread_create(&tid, NULL, class_test, ptr);
    /**
     * 等待结束
     * 注意,如果不加pthread_join,那么有可能新线程还没等创建出来就已经结束程序
     */
    void *retval;
    pthread_join(tid, &retval);
    cout << *(int*)retval << endl;
}

pthread_detach

创建一个线程默认的状态是joinable, 如果一个线程结束运行但没有被join,则它的状态类似于进程中的Zombie Process,即还有一部分资源没有被回收(退出状态码),所以创建线程者应该pthread_join来等待线程运行结束,并可得到线程的退出代码,回收其资源(类似于wait,waitpid)。
但是调用pthread_join后,如果该线程没有运行结束,调用者会被阻塞在有些情况下我们并不希望如此,比如在Web服务器中当主线程为每个新来的链接创建一个子线程进行处理的时候,主线程并不希望因为调用pthread_join而阻塞(因为还要继续处理之后到来的链接),这时可以在子线程中加入代码pthread_datach脱离阻塞,这时候子线程状态为detached,运行结束后会自动释放资源。

pthread_detach只有一个参数:

  • 第一个参数为线程标识符,即线程ID,type: pthread_t
#include <pthread.h>
#include <unistd.h>
#include <iostream>

using namespace std;

void* test(void *p){
    sleep(1000);
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, test, NULL);
    pthread_detach(tid);
    for(int i = 0; i < 1000; i ++){
        sleep(1);
        cout << "sleep " << i << " second." << endl;
    }
}

这里使用pthread_detach,就不会阻塞在testsleep内,会跑进for循环,如果使用的是pthread_join的话,则会等到test函数运行完毕才会往下跑。

pthread_self

pthread_self函数无参数,作用是获取当前线程id


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

using namespace std;

void* test(void *p){
    cout << 'child thread: ' << pthread_self() << endl;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, NULL, test, NULL);
    pthread_detach(tid);
    cout << 'main thread: ' << pthread_self() << endl;
    for(int i = 0; i < 1000; i ++){
        sleep(1);
    }
}

以上代码会输出,主线程和子线程不同的线程id:

main thread: 1
child thread: 2

pthread_once

pthread_once在多线程环境中只执行一次,将多线程环境中只需要执行一次的操作交给 pthread_once执行,则会线程安全的只执行一次。

pthread_once有两个参数:

  • 第一个参数为pthread_once_t变量,相当于标识符,type: pthread_once_t
  • 第二个参数为无参数函数指针,type: void(*func)(void)

以下例子,once_run只会执行一次,但每次执行的结果可能不同,once_run可能在线程1执行,也可能在线程2执行。


#include<iostream>
#include<pthread.h>
#include <unistd.h>
using namespace std;

pthread_once_t once = PTHREAD_ONCE_INIT;

void once_run(void)
{
    cout<<"once_run in thread "<<(unsigned int )pthread_self()<<endl;
}

void * child1(void * arg)
{
    pthread_t tid =pthread_self();
    cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;
    pthread_once(&once,once_run);
    cout<<"thread "<<tid<<" return"<<endl;
}


void * child2(void * arg)
{
    pthread_t tid =pthread_self();
    cout<<"thread "<<(unsigned int )tid<<" enter"<<endl;
    pthread_once(&once,once_run);
    cout<<"thread "<<tid<<" return"<<endl;
}

int main(void)
{
    pthread_t tid1,tid2;
    pthread_create(&tid1,NULL,child1,NULL);
    pthread_create(&tid2,NULL,child2,NULL);
    sleep(10);
    cout<<"main thread exit."<<endl;
    return 0;
}

输出:

thread 2 enter
once_run in thread 2
thread 2 return
thread 3 enter
thread 3 return

pthread_cancel

发送一个cancel信号给指定线程,只有一个参数线程标识符,类型为pthread_t,用法简单,这里不多做介绍,要了解原理的同学可以在linux下用man命令了解更多。

man pthread_cancel

pthread_kill

向某个线程传递一个信号 ,需要在创建的线程中使用signal(SIGKILL,sig_handler)函数处理信号,如果你给一个线程发送了SIGQUIT,但线程却没有实现signal处理函数,则整个进程退出。

pthread_kill两个参数:

  • 第一个参数为线程标记符,type: pthread_t
  • 第二个参数为Linux定义的信号,type:int
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值