码迷,mamicode.com
首页 > 其他好文 > 详细

初始化属性

时间:2014-09-27 02:02:48      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   ar   sp   div   on   

/*初始化属性
int pthread_attr_init(pthread_attr_t *tattr);
函数将对象属性初始化为其缺省值。可能会分配一些存储空间,所以需要下面的函数删除初始化期间分配的存储空间。
int pthread_attr_destroy(pthread_attr_t *tattr);
以上两个函数成功都返回 0.*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
void *consumer(void *p)
{
  static int a = 0;
  printf("<<<<<<<<<<<<<<<<<<<<<(%u),%d\n", (unsigned)pthread_self(), ++a);
  return NULL;
 }       
 int main(int argc, char *argv[])
{
        pthread_t  t1, t2, t3;
        int ret;
        pthread_attr_t attr;
       // pthread_attr_init(&attr);
       //没有这句会发生Segmentation fault (core dumped)
       ret = pthread_create(&t1, &attr, consumer, NULL);
        if(ret != 0)
        {
                 printf("create failed,%d\n", ret);
                 exit(1);
         }       
        pthread_attr_destroy(&attr);
        sleep(1);
    return 0;
}   
/*输出结果:<<<<<<<<<<<<<<<<<<<<<(3077286800),1
注意:属性对象必须初始化,否则属性不能生效,创建线程时将返回错误。
属性对象被销毁,并不影响线程的属性。
pthread_attr_init是初始化一个线程对象的属性,需要用pthread_attr_destroy对其去除初始化
int pthread_attr_init(pthread_attr_t *attr);
返回0,表示函数初始化对象成功。失败时返回一个错误代码。
参数:指向一个线程属性的指针。
attr对应单词attribute 即属性的意思
init对应单词initialize 即初始化的意思*/

 

初始化属性

标签:des   style   blog   color   io   ar   sp   div   on   

原文地址:http://www.cnblogs.com/leijiangtao/p/3995826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!