一起talk C栗子吧(第一百二十回:C语言实例--线程属性)


各位看官们,大家好,上一回中咱们说的是线程死锁的例子,这一回咱们说的例子是:线程属性。闲话休提,言归正转。让我们一起talk C栗子吧!

看官们,我们在前面章回中介绍创建线程时提到过线程和属性,不知道大家还有没有印象。为了方便大家回忆前面章回的内容,我们再次列出创建线程的函数的原型:

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

该函数的第二个参数是pthread_attr_t类型的指针,该指针指向包含有线程属性的变量,这些属性会赋值给新创建的线程。当时我们没有介绍线程的属性,所以使用空指针,表示不修改线程属性。今天我们就来介绍如何使用线程的属性。

线程的属性是pthread_attr_t类型,通常来讲不需要了解该结构体类型的细节。常用的线程属性是分离属性(detachedstate).系统提供了一些函数专门修改该结构体类型中的成员,进而修改线程的属性。我们只需要掌握如何使用函数来修改线程属性就可以。接下来我们介绍一些与线程属性相关的函数。

int pthread_attr_init (pthread_attr_t *__attr)

该函数用来初始化线程属性,初始化的对象是参数中指针所指的变量。

int pthread_attr_setdetachstate (pthread_attr_t *__attr,int __detachstate)

该函数用来设置线程的分离属性(detachdestate),它把参数中指针所指的变量修改为__detachstate的值。

__detachstate的值有两种: PTHREAD_CREATE_JOINABLE和PTHREAD_CREATE_DETACHED。它的默认值是PTHREAD_CREATE_JOINABLE,该值表示不分离线程,而PTHREAD_CREATE_DETACHED表示可分享的线程。这两个在phtread.h文件中被定义为枚举常量,详细如下:

/* Detach state.  */
enum
{
  PTHREAD_CREATE_JOINABLE,
  PTHREAD_CREATE_DETACHED
};

什么是分离线程呢?有看官在提问。分离线程是独立运行的线程,当一个进程创建分离线程后,分离线程独立运行,运行结束后不会返回到创建它的进程中,而且在创建它的进程中不能使用join函数获取分离线程的运行状态。

int pthread_attr_destroy (pthread_attr_t *__attr)

该函数用来释放与线程属性相关的资源。

这三个函数的使用步骤如下

  • 1.定义一个线程属性变量;
  • 2.使用pthread_attr_init函数初始化线程属性变量;
  • 3.使用pthread_attr_setdetachstate函数修改线程属性变量中的分离属性;
  • 4.使用 pthread_create创建线程,并且把新创建的线程的属性值修改为自定义的线程属性值;
  • 5.使用pthread_attr_destroy函数释放与线程属性变量相关的资源;

接下来我们通过具体的例子来说明如何使用这些函数来修改线程的属性,下面是主要的代码:

// init the attr of thread
    res = pthread_attr_init(&attr);

    // set the value of attr
    res = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);

    //create thread using the attr
    printf("Create a new thread \n");

    res = pthread_create(&thread_value,&attr,thread_func,(void *)param);

    printf("Create a new thread is ok,and main thread running continue \n");

    res = pthread_attr_destroy(&attr);


    //wait for thread finished
    res = pthread_join(thread_value,&pthread_res);
    if(0 != res)
    {
        printf("new thread can't be joined\n");
        return 1;
    }
    else
    {
        printf("new thread exit. \n");
    }

    while(flag)
    {
        printf("waiting for new thread exit \n");
        sleep(2);
    }

    printf("main thread exit \n");

下面是新线程的执行函数:

// the new thread function
void *thread_func(void *param)
{
    printf("new thread is running \n");

    flag = 0;
    sleep(1);

    printf("new thread exit \n");

    pthread_exit(&status); // end the thread
}

在代码中我们主是通过属性函数修改了线程的分离属性值,把它从默认值PTHREAD_CREATE_JOINABLE修改为PTHREAD_CREATE_DETACHED,接着通过配置线程的分离属性来创建一个分离线程,下面是程序的运行结果:

The main thread is running   //主线程在运行
Create a new thread          //创建一个新线程
Create a new thread is ok,and main thread running continue 
new thread can't be joined   //不能在主线程中获取新创建线程的状态,主线程在此返回,程序结束

从上面的程序运行结果中可以看到,主线程创建分离线程后不能获取分离线程的运行状态。如果我们把join函数相关的内容注释掉,程序就会有不同的运行结果:

//wait for thread finished
/*  res = pthread_join(thread_value,&pthread_res);
    if(0 != res)
    {
        printf("new thread can't be joined\n");
        return 1;
    }
    else
    {
        printf("new thread exit. \n");
    }
*/

下面是注释掉join函数相关的内容后,程序的运行结果,请大家参考:

The main thread is running    //主线程在运行
Create a new thread           //创建一个新线程
Create a new thread is ok,and main thread running continue 
waiting for new thread exit   //主线程在独立运行,它通过flag标志来判断新线程是否运行结束
new thread is running         //新线程在运行
new thread exit               //新线程运行结束
main thread exit              //主线程运行结束,整个程序运行结束

从上面的程序运行结果中可以看到,主线程和分离线程各自独立运行:主线程通过一个标记来判断新线程是否运行结束,而分离线程运行自己执行函数中的内容,它会修改标志值,当分离线程修改该标记后,主线程就会结束运行。

当然了,这样做还有一个原因就是,如果主线程的结束时间比分离线程的结束时间早的话,两个线程都会结束,因此,让主线程不断运行,直到分离线程运行结束后,主线程在结束运行。

看官们,正文中就不写代码了,完整的代码放到了我的资源中,大家可以点击这里下载使用。

看官们,在本章回中我们主要介绍了线程的分离属性,因为在程序中经常会用到线程的分离属性。线程的其它属性暂时先不介绍了,因为它们的使用方法十分类似。如果在以后章回中有使用线程其它属性的内容,我们再做详细的介绍。

各位看官,关于线程属性的例子咱们就说到这里。欲知后面还有什么例子,且听下回分解 。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值