当前位置:网站首页>Discussion on Service Commitment of Class Objects under Multithreading
Discussion on Service Commitment of Class Objects under Multithreading
2022-07-31 02:41:00 【rainy spring night】
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
引言:
多线程下,哪些需要类本身Handle resource sharing conflicts,哪些需要类调用parties guarantee sharing violations.
类对象 与 调用者 What service commitments should each have?
Let's discuss it below.
问题:
当我们new一个类对象Object后,The object is stored in a pointerpObject;
该pObjectWhat are the service promises of class object pointers.
When we design classes,可能碰到:
- Continuity of service promised externally may be forgotten;
- Sometimes it can also be a transitional design–Transition promises tasks that cannot be fulfilled on their own;
问题是:
- How to distinguish what should be promised in the class?
- How to distinguish which should be promised by the external caller?
服务承诺
I think external callers need a better definition of promises,Except for this part,It is required that the class should promise.
当然这个也不是绝对的,Summarize in the usual sense.
for external callers:
- 类对象指针pObjectThe maintenance must be externally responsible for maintenance,pObjectThe declaration cycle should be maintained externally;
pOjbectPointer lifetimes are usually actively maintained externally,什么时候创建,什么时候销毁; - 类对象构造,Destruction integrity、Uniqueness should be maintained externally;
Not when the class object construction has not been completed,来使用pObject;
Not in a class object destructor call or after destructor,还在使用pObject;
Class object constructor cannot be called more than once/Or destruct multiple times,This should be guaranteed externally;
Constructor and destructor cannot be called simultaneously from different threads,Should be guaranteed externally;
也即同一时刻,构造函数执行 与 Other functions of the class are mutually exclusive,析构函数执行 与 Other functions of the class are mutually exclusive; - When a function in a class requires uniqueness of execution,Same as constructor destructor,Its uniqueness is also maintained externally;
For example, when a function is required to be called,and other functions are mutually exclusive,This type is similar to a constructor destructor,The uniqueness of its execution can be guaranteed externally.
The external promise of the class object:
- 除构造函数、outside the destructor,Call reentrancy of other functions
同一个函数,Reentrancy of calls on different threads requires class objects to guarantee - 除构造函数、outside the destructor,The resources of other function calls are mutually exclusive
不同函数,Mutexes for resource access are invoked on different threads,Should be guaranteed by the class object - 除构造函数、outside the destructor,Mutual exclusion of calls to other functions
Called on a different thread,Mutual exclusion of function calls should be supported by the class object itself through locking and other means. - 除构造函数、outside the destructor,Sequentiality of calls to other functions
There may be a sequential calling relationship between different functions,Class objects are required to organize judgments by means of their own locking status,No crash exception occurs.
典型例子
Sometimes for convenience,Put the lock for external use also inside the class object:
例如:
The following class provides an external lock to the outside world,It is used for external timing processing;
class X
{
public:
// 获取id
uin32_t Get();
// 保存
void Store(info *);
// Acquire an external lock
std::mutex* OuterMutex() {
return &outer_mutex;}
private:
std::mutex inner_mutex;
std::mutex outer_mutex;
}
假如Store与Getpass by itselfinner_mutex支持多线程访问;
But in some cases,We have external requirements,需要Get调用和StoreAbility to do single-threaded access constraints together;
lock();
{
Get(); Store(info);}
unlock();
At this point, we can achieve this through external locks:在每个调用Get & Store的时候,Lock with an external lock,当调用完毕Store后,Release the external lock.
x.OuterMutex().lock();
{
uint32_t m = x.Get();
...
x.Store(&myinfo);
}
x.OuterMutex().unlock();
This is a single-threaded access constraint that binds together part of the function of the class,function can be called from outside,This is achieved through an external lock.
This will not affect the multi-threading support when these functions are accessed individually,Storein discordGet一起时,Each can be accessed by multiple threads.
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)
边栏推荐
- SQL注入 Less54(限制次数的SQL注入+union注入)
- f.grid_sample
- YOLOV5学习笔记(二)——环境安装+运行+训练
- User interaction + formatted output
- Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
- Maximum area of solar panel od js
- To write good test cases, you must first learn test design
- 16. Registration Center-consul
- 经典链表OJ强训题——快慢双指针高效解法
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
猜你喜欢

The effective square of the test (one question of the day 7/29)

字体压缩神器font-spider的使用

mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)

CorelDRAW2022精简亚太新增功能详细介绍

经典链表OJ强训题——快慢双指针高效解法

Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue

Difference between CMOS and TTL?

mmdetection训练一个模型相关命令

Linux下redis7的安装,启动与停止

mysql view
随机推荐
The real CTO is a technical person who understands products
拒绝加班,程序员开发的效率工具集
Clustering index, and what is the difference between a clustering index
汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络
局域网电脑硬件信息收集工具
TCP/IP四层模型
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
YOLOV5学习笔记(三)——网络模块详解
Basic learning about Redis related content
YOLOV5学习笔记(二)——环境安装+运行+训练
开题报告之论文框架
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Mathematical Ideas in AI
16. Registration Center-consul
Unity界面总体介绍
The difference between link and @import
Introduction and use of Drools WorkBench
什么是分布式锁?实现分布式锁的三种方式
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
Verify the integer input