单例模式 - 早期绑定(涉及静态变量)是否减少了互斥锁的需要? [英] Singleton pattern - Does early binding (with static variables involved) diminish the need of mutex locks?

查看:141
本文介绍了单例模式 - 早期绑定(涉及静态变量)是否减少了互斥锁的需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们说早期绑定解决了同步问题。我听不懂怎么样。这是Java还是特别的,或者也适用于C ++?



所以,使用这种方法我们实际上不需要互斥锁?



解决方案

我认为他们是指在开始/创建任何线程之前创建Singleton实例,从而减轻创建时的同步需求。



编辑:添加关于C ++和静态变量的信息



在C ++中,静态变量也在执行之前初始化,如David Harkness提到用于Java。 C ++中的一个问题可以在嵌入式环境中,直到系统初始化之后,才能执行对malloc / new的调用,在这种情况下,使用Singleton静态初始化程序可能会有问题。


They say that early binding solves the problem of synchronization. I couldn't understand "how". Is this something special to Java or the same applies to C++ too?

so, with this method we actually won't require a mutex lock?

解决方案

I think they are referring to creating the Singleton instance before starting/creating any threads, thus alleviating the need for synchronization at creation.

EDIT: adding info about C++ and static variables

In C++, static variables are also initialized before execution like David Harkness mentions for Java. One issue with this in C++ can be in embedded environments where calls to malloc/new cant be performed until after the system is initialized, in which case using a Singleton static initializer could be problematic.

这篇关于单例模式 - 早期绑定(涉及静态变量)是否减少了互斥锁的需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆