posix线程<二>

线程属性:

初始化与销毁属性

int pthread_attr_init(phread_attr_t* attr)

int pthread_attr_destroy(pthread_attr_t* attr)

获取与设置分离属性

int pthread_attr_getdetachstate(const pthread_attr_t* attr, int * detachstate)

int pthread_attr_setdetachstate(pthread_attr_t* attr,int detachstate)

获取与设置栈大小

int pthread_attr_setstacksize(pthread_attr_t* attr,size_t stacksize)

int pthread_attr_getstacksize(pthread_attr_t* attr,size_t* stacksize);

获取与设置继承的调度策略

int pthread_attr_getinheritsched(const pthread_attr_t* attr, int * inheritsched)

int pthread_attr_setinheritsched(pthread_attr_t* attr, int inheritsched)

获取与设置调度参数

int pthread_attr_getschedparam(const pthread_attr_t* attr, struct sched_param* param)

int pthread_attr_setschedparam(pthread_attr_t* attr, const struct sched_param* param)

获取与设置并发级别

int pthread_setconcurrency(int new _level)

int pthread_getconcurrency(void)

仅在N:M线程模型中有效,设置并发级别,给内核一个提示:表示提供给定级别数量的核心线程来映射用户线程是高效的


线程特定数据:

1. 在单线程程序中,我们经常用到“全局变量”以实现多个函数间共享数据

2. 在多线程环境下,由于数据是共享的,因此全局变量也为所有线程共有

3. 但有时应用程序设计中有必有提供线程私有的全局变量,仅在某个线程中有效,但却可以跨多个函数访问

4.POSIX线程库通过维护一定的数据结构来解决这个问题,这个数据称为(thread-specific Data,或TSD)

int pthread_key_create(pthread_key_t* key,void (*destructor)(void));创建一个key ,也就是寻找一个空位(每个线程都有128个key)

int pthread_key_delete(pthread_key_t key )

void * pthread_getspecific(pthread_key_t key)设置特殊数据

int pthread_setspecific(pthread_key_t key,const void* value)

int pthread_once(pthread_once_t * once_control void(*init_routine)(void))

pthread_once_t once_control = PTHREAD_ONEC_INIT;


#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>


pthread_key_t  key_tsd;

//创建数据
typedef struct tsd
{
	pthread_t tid;
	char* str;
}tsd_t;

void destroy_routine(void *value)
{
	printf("destroy.........\n");
	free(value);
}
void* thread_rontine(void* arg)
{
	tsd_t * value = (tsd_t *)malloc(sizeof(tsd_t));
	value->tid = pthread_self();
	value->str = (char*)arg;

	pthread_setspecific(key_tsd,value);	
	printf("%s setspecific %p\n",(char*)arg,value);
	value = pthread_getspecific(key_tsd);
	printf("tid = 0x%x str=%s\n",(int)value->tid,value->str);

	sleep(2);

	value = pthread_getspecific(key_tsd);
	printf("tid = 0x%x str=%s\n",(int)value->tid,value->str);
	return NULL;
}

int  main()
{
	pthread_key_create(&key_tsd,thread_rontine);

	pthread_t tid1;
	pthread_t tid2;
	pthread_create(&tid1,NULL,thread_rontine,"pthread1");
	pthread_create(&tid2,NULL,thread_rontine,"pthread2");

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);
	pthread_key_delete(key_tsd);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值