singleton

C++ Meyers Singleton - thread safe (code equivalent for mutex?)

邮差的信 提交于 2020-01-17 05:08:09
问题 I was pointed last week about a piece of code like this: #include <pthread.h> namespace NSTest { class SingletonClass { public: static SingletonClass & getInstance() { static pthread_mutex_t mutex; pthread_mutex_lock(&mutex); if(singletonPtr==nullptr) { createInstence(); } return (*singletonPtr); pthread_mutex_unlock(&mutex); } private: static void createInstence() { static SingletonClass static_SingletonClass; singletonPtr=&static_SingletonClass; } static SingletonClass * singletonPtr; };

C++ Meyers Singleton - thread safe (code equivalent for mutex?)

风流意气都作罢 提交于 2020-01-17 05:08:06
问题 I was pointed last week about a piece of code like this: #include <pthread.h> namespace NSTest { class SingletonClass { public: static SingletonClass & getInstance() { static pthread_mutex_t mutex; pthread_mutex_lock(&mutex); if(singletonPtr==nullptr) { createInstence(); } return (*singletonPtr); pthread_mutex_unlock(&mutex); } private: static void createInstence() { static SingletonClass static_SingletonClass; singletonPtr=&static_SingletonClass; } static SingletonClass * singletonPtr; };

PHP static not so static

早过忘川 提交于 2020-01-16 18:15:51
问题 I've noticed that the keyword static in PHP is not that static at all. Lets say Elmo is my singleton: class Elmo { private static $instance; private function __construct() { echo 'Elmo says constructor\n'; } public static function getInstance() { if (!isset(self::$instance)) self::$instance = new Elmo(); return self::$instance; } public function boo() { echo 'Elmo says boo!\n'; } } And the following file is just a regular .php script. <?php Elmo::getInstance()->boo(); Elmo::getInstance()->boo

Creating an async singletone in javascript

安稳与你 提交于 2020-01-16 08:36:23
问题 I need to implement an async singleton, that creates a single instance of a class that requires asynchronous operations in order to pass arguments to the constructor. I have the following code: class AsyncComp { constructor(x, y) { this.x = x; this.y = y; } // A factory method for creating the async instance static async createAsyncInstance() { const info = await someAsyncFunction(); return new AsyncComp(info.x, info.y); } // The singleton method static getAsyncCompInstance() { if

Creating an async singletone in javascript

Deadly 提交于 2020-01-16 08:36:03
问题 I need to implement an async singleton, that creates a single instance of a class that requires asynchronous operations in order to pass arguments to the constructor. I have the following code: class AsyncComp { constructor(x, y) { this.x = x; this.y = y; } // A factory method for creating the async instance static async createAsyncInstance() { const info = await someAsyncFunction(); return new AsyncComp(info.x, info.y); } // The singleton method static getAsyncCompInstance() { if

Would a singleton GUI be good in this case?

半腔热情 提交于 2020-01-16 07:04:08
问题 I am working on a project where I have lots of classes referring to my GUI (mainly the panels but sometimes the frame itself). So I was thinking that instead of passing the frame as an argument to every constructor and create getters in each class, I would make a singleton instance of the JFrame so all the classes get access to it instead. Is this a good approach or will it just punish my laziness in some way? Edit: I'm not just lazy, I'm trying to think in models here: For example, let's say

Singleton QNetworkAccessManager not directing the response to desired form

非 Y 不嫁゛ 提交于 2020-01-16 01:13:37
问题 In a small Qt application, I have a NetworkAccessManager(NAM) wrapped in a singleton class. NAM object of this class is used by four independent form classes. Each form class fires a request to a separate php (and php takes at least 3 seconds with sleep(3)). Now there are two cases: Case I: When NAM, for each form, is connected to form's slot IN CONSTRUCTOR. In this case, when i send requests from all four forms simultaneously; all the responses are directed to just one form (the one that was

Singleton QNetworkAccessManager not directing the response to desired form

爷,独闯天下 提交于 2020-01-16 01:13:12
问题 In a small Qt application, I have a NetworkAccessManager(NAM) wrapped in a singleton class. NAM object of this class is used by four independent form classes. Each form class fires a request to a separate php (and php takes at least 3 seconds with sleep(3)). Now there are two cases: Case I: When NAM, for each form, is connected to form's slot IN CONSTRUCTOR. In this case, when i send requests from all four forms simultaneously; all the responses are directed to just one form (the one that was

iOS - Passing accumulating data down a long chain of UIViewControllers

喜夏-厌秋 提交于 2020-01-15 12:23:47
问题 I am writing an iOS app with several (maybe 8) survey-style modules where the user will answer a series of questions (on Family Life, Health, etc.). Some modules have as many as 12 questions. Each question has its own nib and its own UIViewController. The answers will be written to a database, ideally, at the end of the module. Each module corresponds with one record in one table. There will only ever be one user, hence, one record. I want to leave the Core Data stack in the app delegate. I

iOS - Passing accumulating data down a long chain of UIViewControllers

丶灬走出姿态 提交于 2020-01-15 12:23:25
问题 I am writing an iOS app with several (maybe 8) survey-style modules where the user will answer a series of questions (on Family Life, Health, etc.). Some modules have as many as 12 questions. Each question has its own nib and its own UIViewController. The answers will be written to a database, ideally, at the end of the module. Each module corresponds with one record in one table. There will only ever be one user, hence, one record. I want to leave the Core Data stack in the app delegate. I