From b02e7e507e4d7a894a78189d93403d23bddc0074 Mon Sep 17 00:00:00 2001 From: hejun Date: Sun, 15 Jun 2025 07:02:48 -0400 Subject: [PATCH] create design dir to learning design mode --- .vscode/c_cpp_properties.json | 15 ++++++++++ .vscode/settings.json | 55 +++++++++++++++++++++++++++++++++++ src/design/single.c | 27 +++++++++++++++++ src/design/single.cc | 19 ++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json create mode 100644 src/design/single.c create mode 100644 src/design/single.cc diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..5d907b5 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,15 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/**" + ], + "defines": [], + "compilerPath": "/usr/bin/clang-18", + "intelliSenseMode": "linux-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..263663c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,55 @@ +{ + "files.associations": { + "cctype": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "charconv": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdint": "cpp", + "deque": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "span": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "typeinfo": "cpp", + "variant": "cpp", + "format": "cpp", + "text_encoding": "cpp" + } +} \ No newline at end of file diff --git a/src/design/single.c b/src/design/single.c new file mode 100644 index 0000000..1b10d9b --- /dev/null +++ b/src/design/single.c @@ -0,0 +1,27 @@ +#include +#include + +#include + +struct Singleton +{ +}; + +struct Singleton *getInstance() +{ + static atomic_flag flag = ATOMIC_FLAG_INIT; + + static struct Singleton *instance = NULL; + + if (!instance) + { + if (atomic_flag_test_and_set(&flag) == 0) + { + instance = malloc(sizeof(struct Singleton)); + } + while (!instance) + ; + } + + return instance; +} \ No newline at end of file diff --git a/src/design/single.cc b/src/design/single.cc new file mode 100644 index 0000000..46315b1 --- /dev/null +++ b/src/design/single.cc @@ -0,0 +1,19 @@ +#include + +class Singleton +{ + + Singleton(const Singleton &) = delete; + Singleton &operator=(const Singleton &) = delete; + + static Singleton &getInstnce() + { + static Singleton instance; + return instance; + + } + +private: + Singleton() {} + ~Singleton() {} +}; \ No newline at end of file