Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of single case lazy and multithreaded by C++

Shulou Source: shulou.com Published: 2022-06-01 18:44:57 09月11日 Update

This article mainly shows you "how C++ solves single-case lazy and multithreaded problems". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn this article "how C++ solves single-case lazy and multithreaded problems".

Single-case lazy and multithreaded problems

As a singleton mode, only one memory space is created during the entire program run, in order to achieve this goal.

1. You need to set the constructor as a private member

2. You need a private static pointer to point to yourself

3. You need a public static function to expose the static pointer above.

The following code is an example of a slacker.

Click (here) to collapse or open

# include

Using namespace std

Class single_ins

{

Private:

Int a

Int b

Single_ins ()

{

A = 0

B = 0

}

Static single_ins* myc

Public:

Void setval (const int& a minute Const int& b)

{

This- > a = a

This- > b = b

}

Void print ()

{

Cout

Tags: Slackers threads problems pointers functions static memory multiple that is members time space protection checking clocking code content variables patterns articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Xiaomi OPPO Reno vpn MySQL