Thu, Oct 06 2005

Code Snippet Released - Thread-safe Singleton (for Windows platforms)

Here is an implementation of a thread-safe* C++ singleton template. It's small, simple, and easy to use.

You can download the code snippet from here: Singleton.zip

* Only construction of the class instance created by the singleton is made thread-safe. Using members of the class are not specifically made thread-safe, and remains the programmers responsibility if necessary.

1 comment(s)

Andreas (link) wrote @

Using Sleep()
The code should be using Sleep(0) instead of Sleep(1). According to the documentation: A value of zero causes the thread to relinquish the remainder of its time slice to any other thread of equal priority that is ready to run. If there are no other threads of equal priority ready to run, the function returns immediately, and the thread continues execution. Which is exactly what the code is trying to accomplish. Besides that, nice job.

 

Add a comment:








Copyright ©2004-2009 O. Patrick Barnes. All rights reserved.  

Validate XHTML Validate CSS Validate RSS Green Web Hosting! This site hosted by DreamHost.