Singleton Design Pattern with thread safety in C++

preview_player
Показать описание
- How to create a singleton design pattern both in classical C++ and Modern C++
- How to create singleton using pointer and make it thread safe with std::mutex
- How to create singleton using reference and thread safe without even using std::mutex (Meyer's Singleton design pattern)
#cpp #designpatterns #singleton #meyers #meyerssingleton #programming #thread #multithreadingincpp #c++ #linux #gcc #g++
Рекомендации по теме
Комментарии
Автор

Why u stop explain design patterns?
I think you are best person to explain that u actually did this video very simple to understand please do more videos bro

mizel_almizel
Автор

I don't comment on videos often but this video was extremely helpful man. You delivered the information in a very quick and easy-to-understand way. Would love to see you continue making videos covering more programming concepts! Thank you for this!

tyferguson
Автор

This is gold. Your example and narrative are very clear and simple to understand. You are a talented teacher. Thank you for sharing. Regards from the UK 👍

NotMarkKnopfler
Автор

thanks. just to add that i assume here, we are saying that only the CREATION of the static singleton is threadsafe. However, once you have got the reference of the Singleton, you still need to consider the thread-safety on the operation on this object.

chankayau
Автор

What an explanation!! Thank you so much!! When can we expect more design patterns videos??

pratikshar
Автор

Very well explain, you have to make copy contructor, assignment operator private also.

pankajkushwaha
Автор

One thing that we lose in the modern way is controlling the lifetime of the singleton.

Before we had the control to destroy things.

One issue with static vars is they will be destroyed when the program goes down. And we need to be careful about what we destroy during that time.

Even Microsoft warns about these things.

IMHO, Old way is much safer because of lifetime control. And if our program doesn’t call get instance 100’s of times I think performance wouldn’t be too bad.

What do you think?

venkateshpalanivel
Автор

Hi thanks for this videos, how we can use this as object for example there is an member variable m_height, method setHeight(), how we can call setHeight() here

dhinagaranebi
Автор

1 #include <iostream>
2
3 using namespace std;
4
5 class Singleton{
6 private:
7 Singleton(){
8 std::cout << "C'TOR" << endl;
9 }
10 ~Singleton(){
11 std::cout << "D'TOR" << endl;
12 }
13 static Singleton* m_pInstance;
14 public:
15 static Singleton* CreateInstance(){
16 if(m_pInstance == nullptr)
17 {
18 m_pInstance = new Singleton;
19 }
20 return m_pInstance;
21 }
22 static void Destroy(){
23 if(m_pInstance != nullptr){
24 delete m_pInstance;
25 m_pInstance == nullptr;
26 }
27 }
28 };
29 Singleton* Singleton::m_pInstance = nullptr;
30
31 int main()
32 {
33 Singleton::CreateInstance();
34 Singleton::Destroy();
35 Singleton::Destroy();
36 Singleton::Destroy();
37 Singleton::Destroy();
38 Singleton::Destroy();
39 return 0;
40
41 }
Crashing as below output
C'TOR
D'TOR
D'TOR
free(): double free detected in tcache 2
Aborted (core dumped)
Am I missing something ??

rohitjmics
Автор

for the love of God dont use black and then a white background! that damn thing hurts!
also you need to delete the copy and move constructors so someone doesnt create chaos unintentionally!

amortalbeing