当前位置:网站首页>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)
边栏推荐
- Force buckled brush the stairs (7/30)
- SQL注入 Less47(报错注入) 和Less49(时间盲注)
- Intranet Infiltration - Privilege Escalation
- General introduction to the Unity interface
- 【银行系列第一期】中国人民银行
- STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
- First acquaintance with C language -- array
- 关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
- 7、私信列表
- Drools Rule Properties, Advanced Syntax
猜你喜欢
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
mysql view
Drools basic introduction, introductory case, basic syntax
经典链表OJ强训题——快慢双指针高效解法
Linux下redis7的安装,启动与停止
4、敏感词过滤(前缀树)
The effective square of the test (one question of the day 7/29)
什么是分布式锁?实现分布式锁的三种方式
Introduction and use of Drools WorkBench
STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
随机推荐
f.grid_sample
Verify the integer input
The difference between link and @import
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
Linux下redis7的安装,启动与停止
系统需求多变如何设计
Mathematical Ideas in AI
StringJoiner in detail
多线程下类对象的服务承诺探讨
Teach you how to configure Jenkins automated email notifications
mmdetection trains a model related command
try-catch中含return
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
ShardingJDBC usage summary
19.支持向量机-优化目标和大间距直观理解
AtCoder Beginner Contest 261 Partial Solution
分布式与集群是什么 ? 区别是什么?
8、统一处理异常(控制器通知@ControllerAdvice全局配置类、@ExceptionHandler统一处理异常)
Face detection based on opencv
直播预告 | KDD2022博士论文奖冠亚军对话