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
    评论
这些头文件是C++编程中常用的库文件,主要作用如下: - #include <iostream>:用于输入输出流的操作,如输出到控制台等。 - #include "ros/ros.h":ROS机器人操作系统的头文件,用于机器人程序的编写。 - #include "std_msgs/String.h":ROS中的标准消息类型,用于传递字符串类型的消息。 - #include "std_msgs/Float32.h":ROS中的标准消息类型,用于传递浮点型数据。 - #include <sstream>:用于字符串流的操作,如将数字转换为字符串等。 - #include "math.h":数学库文件,提供了许多常用的数学函数。 - #include "can_drive/ICANCmd.h":自定义消息类型,用于控制CAN总线通信。 - #include "can_drive/Nav_data_msg.h":自定义消息类型,用于传递导航数据。 - #include "can_drive/encoder_vel_msg.h":自定义消息类型,用于传递编码器速度数据。 - #include "can_drive/radar_obs_msg.h":自定义消息类型,用于传递雷达障碍物数据。 - #include "can_drive/camera_obs_msg.h":自定义消息类型,用于传递摄像头障碍物数据。 - #include "sensor_msgs/Imu.h":ROS中的标准消息类型,用于传递IMU数据。 - #include "can_drive/pwm_cmd_msg.h":自定义消息类型,用于控制PWM信号输出。 - #include "can_drive/sensor_states_msg.h":自定义消息类型,用于传递各种传感器状态数据。 - #include <tf/tf.h>:ROS中的变换库文件,用于实现坐标系变换。 - #include <bitset>:进制库文件,提供了位运算的一些常用函数。 - #include <string>:字符串库文件,提供了许多字符串操作的函数。 - #include <algorithm>:STL算法库文件,提供了许多常用的算法函数。 - #include <thread>:C++11中的多线程库文件,用于实现多线程编程。 - #include <pthread.h>:POSIX线程库文件,用于实现多线程编程。 - #include <time.h>:时间库文件,提供了许多时间相关的函数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值