Chinaunix首页 | 论坛 | 博客
  • 博客访问: 46556
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 106
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-11 11:04
文章分类

全部博文(17)

文章存档

2014年(17)

我的朋友

分类: LINUX

2014-10-17 12:10:30

1.detach实例:

//!> detach

#include
#include
#include

void * entrance( void * arg )
{
    int                     get_attr_int;
    pthread_attr_t        attr;

    printf("子进程创建OK, ID ==  %d\n", (unsigned)pthread_self());

    if( pthread_attr_getdetachstate( &attr, &get_attr_int ) != 0 )
    {
        printf("获取属性失败...\n");
        exit( EXIT_FAILURE );
    }
   
    if( PTHREAD_CREATE_DETACHED == get_attr_int )        //!> 设置分离OK
    {
        printf("我是被分离的线程...\n");
    }
   
}

int main( int argc, char ** argv )
{
    pthread_t            tid;
    pthread_attr_t        attr;
   
    pthread_attr_init( &attr );            //!> 初始化属性
   
    if( pthread_attr_setdetachstate( &attr, PTHREAD_CREATE_DETACHED ) != 0 )
    {
        printf("设置属性失败...\n");
        exit( EXIT_FAILURE );
    }   
                                        //!> 设置成分离
    if( pthread_create( &tid, &attr, entrance, NULL ) !=0 )    //!> 按照此属性创建线程
    {
        printf("创建线程失败...\n");
        exit( EXIT_FAILURE );
    }
    else
    {
        printf("创建OK... \n");
    }
               
    //!> pthread_join(tid);            //!> 注意此处的join是不需要的!!!!!!!
    pthread_attr_destroy( &attr );    //!> 去除属性
   
    sleep( 2 );            //!> 注意此处要有一个释放CPU处理,不然看不到子线程输出
   
    return 0;
}

        运行结果:
                创建OK...
                子进程创建OK, ID ==  1635653376
                我是被分离的线程...


2.继承、优先级、调度策略 实例

//!> 继承、线程优先级、调度策略

#include
#include
#include

void * entrance( void * arg )
{
    int        policy;            //!> 调度策略
    int        max_;            //!> 最大优先级
    int         min_;             //!> 最小优先级
   
    struct sched_param        param;        //!> 参数
    pthread_attr_t                attr;        //!> 属性
   
    pthread_attr_init( &attr );        //!> 初始化

    //!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //!> 下面设置继承性
   
    pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
                                    //!> 设置继承性
    pthread_attr_getinheritsched( &attr, &policy );
                                    //!> 得到i继承属性
    if( PTHREAD_EXPLICIT_SCHED == policy )
    {
        printf("继承属性是:PTHREAD_EXPLICIT_SCHED\n");
    }
    else if( PTHREAD_INHERIT_SCHED == policy )
    {
        printf("继承属性是:PTHREAD_INHERIT_SCHED\n");
    }
    else
    {
        printf("继承属性Error...\n");
        exit( EXIT_FAILURE );
    }
   
    //!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //!> 下面设置调度策略
   
    pthread_attr_setschedpolicy( &attr, SCHED_RR );    //!> 设置轮转法
    pthread_attr_getschedpolicy( &attr, &policy );        //!> 获得...
   
    if( SCHED_FIFO == policy )
    {
        printf("调度算法:SCHED_FIFO\n");
    }
    else if( SCHED_RR == policy )
    {
        printf("调度算法:SCHED_RR\n");
    }
    else if( SCHED_OTHER == policy )
    {
        printf("调度算法:SCHED_OTHER\n");
    }
    else
    {
        printf("调度算法Error....\n");
        exit( EXIT_FAILURE );
    }
   
    //!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    //!> 下面优先级设置
   
    max_ = sched_get_priority_max( policy );    //!> 得到系统最大优先级值
    min_ = sched_get_priority_min( policy );        //!> 得到系统最小优先级值
   
    printf("系统最大优先级值: %d \n", max_);
    printf("系统最小优先级值: %d \n", min_);
   
    param.sched_priority = max_;
    pthread_attr_setschedparam( &attr, ¶m );    //!> 设置param
    pthread_attr_getschedparam( &attr, ¶m );    //!> 获得param
   
    printf("优先级数 == %u\n", param.sched_priority);
   
    pthread_attr_destroy( &attr );

}

int main( int argc, char ** argv )
{
    pthread_t    tid;
   
    pthread_create( &tid, NULL, entrance, NULL );

    pthread_join( tid, NULL );    //!> 默认是非分离状态,所以要join

    return 0;
}

3.作用域、stack大小及地址、警戒缓冲区 实例


//!> 作用域、stack大小及地址、警戒缓冲区 实例

#include
#include
#include
#include

void * entrance( void * arg )
{
    pthread_attr_t        attr;
    int                     scope;        //!> 作用域
    size_t                stack_size;    //!> 栈大小
    void          *    stack_addr;    //!> 栈地址
    size_t              guardsize;    //!> 警戒缓冲区大小
   
    pthread_attr_init( &attr );
   
    pthread_attr_getscope( &attr, &scope );    //!> 得到scope
    if( PTHREAD_SCOPE_PROCESS == scope )
    {
        printf("Scope == PTHREAD_SCOPE_PROCESS\n");
    }
    else if( PTHREAD_SCOPE_SYSTEM == scope )
    {
        printf("Scope == PTHREAD_SCOPE_SYSTEM\n");
    }
    else
    {
        printf("Scope Error...");
        exit( EXIT_FAILURE );
    }
   
    pthread_attr_getstacksize( &attr, &stack_size );            //!> 得到栈大小
    printf("Stack Size == %d \n", (int)stack_size);
   
    pthread_attr_getstackaddr( &attr, &stack_addr );            //!> 得到栈地址
    printf("Stack address == %s \n", ( char * )stack_addr );
   
    pthread_attr_getguardsize( &attr, &guardsize );            //!> 得到警戒区大小
    printf("Guardsize == %d\n", (int)guardsize);
   
    pthread_attr_destroy( &attr );
}

int main( int argc, char ** argv )
{
    pthread_t        tid;
   
    if( pthread_create( &tid, NULL, entrance, NULL ) != 0 )
    {
        printf("创建线程失败...\n");
        exit( EXIT_FAILURE );
    }
   
    pthread_join( tid, NULL );
   
    return 0;
}
阅读(1493) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~