分享

线程函数的详细使用

 海漩涡 2016-04-10

//libpthread.c

#include "libpthread.h"

/*  
    parameter:

OUT:
    1 : pthread_t   线程ID

INPUT:
    2 : route       线程运行函数
    3 : param       线程运行函数的参数
    4 : scope       线程资源竞争范围 0,系统默认 1,与系统所有线程  2,与同一进程
    5 : detach      线程状态: 0,系统默认 1,joinable 2,detach
*/
int CreateRouteThread(pthread_t *thread,FuncRouteThread route, void *param, int scope, int detach)
{
    int ret = -1;
    pthread_attr_t attr;
    pthread_attr_init(&attr);
    
    if(1 == scope)
    {
        /*表示与系统中所有线程一起竞争资源,如CPU时间*/
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
    }
    else if(2 == scope)
    {
        /*仅与同进程中的线程竞争资源*/ 
        pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
    }

    if(1 == detach)
    {
         /* 线程能用pthread_join()来阻塞,系统默认PTHREAD_CREATE_JOINABLE状态*/
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
  
    }
    else if(2 == detach)
    {
        /*线程不能用pthread_join()来阻塞,在退出时自行释放所占用的资源*/
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
    }
    
    ret = pthread_create(thread, &attr, route, param);

    /*
    Destroying  a thread attributes object has no
    effect on threads that were created using that object.
    不需要改变线程属性后destroy,此时对线程的运行无影响
    */
    pthread_attr_destroy(&attr);
    
    return ret;
}


=========================================================================


//libpthread.h

#ifndef _LIBPTHREAD_H_
#define _LIBPTHREAD_H_

#include <pthread.h>
#include <stdio.h>

/*
    线程相关功能函数

    1、int pthread_join(pthread_t thread, void **retval);
       阻塞到线程ID为thread的线程结束,并捕捉线程的pthread_exit()返回值到retval,线程默认joinable
    
       The  pthread_join()  function  waits for the thread specified by thread to terminate.
    If that thread has already terminated, then pthread_join() returns immediately.   The
    thread specified by thread must be joinable.



    2、void pthread_exit(void *retval);
       停止当前线程,并返回值retval此值被pthread_join捕获(thread must be joinable)
       【注:retval不能为局部变量,否则pthread_exit捕获的是已经释放的值,出错】


    3、int pthread_detach(pthread_t thread);
       将线程设置为detach状态,当线程结束后自动释放资源。此时线程不能被pthread_join即为unjoinable


    4、int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
       设置线程创建时为detach还是joinable状态,此函数在pthread_create前
    
       detachstate:
       PTHREAD_CREATE_DETACHED  线程创建的时候为detach状态 

       PTHREAD_CREATE_JOINABLE  线程创建的时候为joinable状态 【系统默认】

       获得线程状态
       int pthread_attr_getdetachstate(pthread_attr_t *attr, int *detachstate);


    5、int pthread_cancel(pthread_t thread);
       向某线程发送取消消息,即停止某线程。线程能否被取消取决于stat(默认enable)和type(默认deferred)



    6、int pthread_setcancelstate(int state, int *oldstate);
       设置当前线程是否可被pthread_cancel取消

       PTHREAD_CANCEL_ENABLE 可被取消 【系统默认】
       PTHREAD_CANCEL_DISABLE 不可


    7、int pthread_setcanceltype(int type, int *oldtype);
       设置当前线程被 pthread_cancel取消的类型

       PTHREAD_CANCEL_DEFERRED       延期到调用下一个函数才cancel【系统默认】
       PTHREAD_CANCEL_ASYNCHRONOUS   立即被取消


    8、pthread_t pthread_self(void);
       返回当前线程ID


       
    9、在线程异常结束时用于释放资源(mutex、free等),防止内存泄漏和死锁
       例被pthread_cancel、pthread_exit终止

       将释放资源函数入栈
       void pthread_cleanup_push(void (*routine)(void *),void *arg);

       routine:释放资源函数      arg:释放资源函数的参数
       【注:arg可为局部变量,因为pthread_cleanup_pop会运行完routine】

       将释放资源函数出栈
       void pthread_cleanup_pop(int execute); 

       execute: 0 ,不运行routine 。 其他,运行routine



    10、设置和获取线程竞争资源的范围:整个系统,还是当前线程
       int pthread_attr_setscope(pthread_attr_t *attr, int scope);
       scope:
       PTHREAD_SCOPE_SYSTEM    整个系统【系统默认】
       PTHREAD_SCOPE_PROCESS   当前线程
       
       int pthread_attr_getscope(pthread_attr_t *attr, int *scope);


    11、线程属性:
        pthread_attr_t attr;

        初始化线程属性,给pthread_attr_setscope、pthread_attr_setdetachstate等使用
        然后在pthread_creat后生效
        int pthread_attr_init(pthread_attr_t *attr);

        销毁此属性变量,不会改变线程的任何属性,不会对线程的运行有任何影响
        int pthread_attr_destroy(pthread_attr_t *attr);



    12、设置线程栈大小
        int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

        获取线程栈大小
        int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *stacksize);


    
    13、其他不常用功能函数
        int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy);
        int pthread_attr_getschedpolicy(pthread_attr_t *attr, int *policy);
      pthread_testcancel(3), 
        pthread_attr_setaffinity_np(3), 
        pthread_attr_setinheritsched(3),
        pthread_attr_setschedparam(3),   
        pthread_attr_setschedpolicy(3),
         
*/


/* 线程函数 */
typedef void *(*FuncRouteThread)(void *);

int CreateRouteThread(pthread_t *thread,FuncRouteThread route, void *param, int scope, int detach);

#endif




    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多