Overview
Features
Download
Documentation
Community
Add-Ons & Services

WinCE Poco::SingletonHolder Threadsafe

Please post support and help requests here.

WinCE Poco::SingletonHolder Threadsafe

Postby buettner » 19 Jun 2013, 17:19

Hi,

is it possible that the Poco::SingletonHolder<..> ist not Threadsafe under WinCE 6?
I use the same code with Windows X86, Linux X86 and Linux ARM but on WinCE i have problems here!

Code: Select all
  static MessageDispatcherSingleton& GetInstance() {
        static Poco::SingletonHolder<MessageDispatcherSingleton> sh;
        return *sh.get();
    }


Has anyone an advice?

Christian
buettner
 
Posts: 5
Joined: 18 Jun 2013, 10:23

Re: WinCE Poco::SingletonHolder Threadsafe

Postby alex » 19 Jun 2013, 17:34

buettner wrote:is it possible that the Poco::SingletonHolder<..> ist not Threadsafe under WinCE 6?

It is possible because, strictly speaking, singleton is not thread safe under any operating system.
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: WinCE Poco::SingletonHolder Threadsafe

Postby guenter » 19 Jun 2013, 20:42

Initialization of static locals is not threadsafe prior to C++11, according to the standard. It definitely is not threadsafe with Microsoft compilers, except with VC++ 2012 if C++11 mode is enabled. It is threadsafe with GCC on most platforms, though.

In contrast, initialization of non-local statics is threadsafe with most compilers, including VC++.

The "hack" to make SingletonHolder threadsafe thus is:

Code: Select all
namespace
{
        static Poco::SingletonHolder<MessageDispatcherSingleton> sh;
}

static MessageDispatcherSingleton& GetInstance() {
        return *sh.get();
}


It's not pretty, but it does the job.
guenter
 
Posts: 1133
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: WinCE Poco::SingletonHolder Threadsafe

Postby buettner » 20 Jun 2013, 08:48

Cool - thank you for the explanation!
I try it and leave a comment if it worked..
buettner
 
Posts: 5
Joined: 18 Jun 2013, 10:23

Re: WinCE Poco::SingletonHolder Threadsafe

Postby buettner » 20 Jun 2013, 10:35

Thats it.. now it works! Thank you..

in Header File:
Code: Select all
static MessageDispatcherSingleton& GetInstance();


in CPP:
Code: Select all
static Poco::SingletonHolder<MessageDispatcherSingleton> sh;
...
MessageDispatcherSingleton& MessageDispatcherSingleton::GetInstance() {
    return *sh.get();
}
buettner
 
Posts: 5
Joined: 18 Jun 2013, 10:23


Return to Support

Who is online

Users browsing this forum: No registered users and 3 guests