Discussion:
[ace-users] Thread safe singleton class example with ACE
(too old to reply)
Douglas C. Schmidt
17 years ago
Permalink
Hi Ramesh,

The typical way of getting a thread-safe singleton is to use

typedef ACE_Singleton <Your_Class_Goes_Here, ACE_SYNCH_RECURSIVE_MUTEX> MY_SINGLETON;

There are various examples of this in ACE_ROOT/tests if you search for
ACE_Singleton.

Take care,

Doug
I am a newbie to ACE, is there a thread-safe singleton implementation
example in the ACE tarball? I havent exhausted searching the book yet,
but checking to know if there is something already there in the
examples - which I can look at and understand. BTW I have the 5.6.5
version of ace installed.
Thanks
/R
--
Dr. Douglas C. Schmidt Professor and Associate Chair
Electrical Engineering and Computer Science TEL: (615) 343-8197
Vanderbilt University WEB: www.dre.vanderbilt.edu/~schmidt
Nashville, TN 37203 NET: ***@vanderbilt.edu
T0mServ0
17 years ago
Permalink
Since ACE uses a template for the ACE_Singleton type, would this be a
problem when other DLL's in the same process try to use it?

For example:

Let's suppose there is a DLL called libmysingleton.so which actually
implements the singleton in question (we'll call it MySingleton).

There is a common header file which defines the singleton like so
(MySingleton.h):

class MySingleton
{
friend class ACE_Singleton<MySingleton, ACE_Recursive_Thread_Mutex>;
public:
void test();

private:
MySingleton();
~MySingleton();
};
typdef ACE_Singleton<MySingleton, ACE_Recursive_Thread_Mutex> MYSINGLETON;

Now let's say I have another DLL called libtest2.so which has some code
which USES the singleton (anywhere in the code doesn't matter):

... MYSINGLETON::instance()->test(); ...

And I have created an EXE called test which links with libtest2.so and
libmysingleton.so, and in the test source I also use the singleton:

... MYSINGLETON::instance()->test();

The test source, libtest2.so source, and libmysingleton.so source all share
the same common header file MySingleton.h.

The problem I'm seeing is that if I understand templates correctly, if each
separate module shares the same header file, there will be multiple
instances of the MYSINGLETON created due to the templates.
I have a printf() statement in the constructor of MySingleton() which indeed
tells me that more than one instance of MySingleton is created when I run
the test program. The instance in just the test module is created first,
and when libtest2.so uses it, a new instance is created there too.

Now, is there anything or compiler flag I need to be using to prevent this?
Or do you see anything else wrong or any changes I could make to make
certain that only one instance of MySingleton is ever created under one
process?

Thanks!
Brandon
...
T0mServ0
17 years ago
Permalink
Apologies,

I meant to say that I'm seeing this issue on Linux Redhat which has GCC
4.2.4. I'm using ACE 5.6.5.

Brandon
...
T0mServ0
17 years ago
Permalink
I narrowed down the issue to understand that it has something to do with
the -fvisibility=hidden and -fvisibility-inlines-hidden flags. I do have
the MySingleton class "exported":

class __attribute__((visibility("default"))) MySingleton
{
......
};

I'll keep playing around with it...

If I take out the -fvisibility=hidden -fvisibility-inlines-hidden flags,
only one copy of the singleton class created.

Brandon
...
Johnny Willemsen
17 years ago
Permalink
Hi,

Check ace/Based_Pointer_Repository.h for a singleton where there is one
instance per process.

Regards,


Johnny Willemsen
Remedy IT
Postbus 101
2650 AC  Berkel en Rodenrijs
The Netherlands
www.theaceorb.nl / www.remedy.nl 

*** Integrated compile and test statistics see
http://scoreboard.theaceorb.nl ***
*** Commercial service and support for ACE/TAO/CIAO             ***
*** Get your free TAO Programmers Guide copy from
http://www.theaceorb.nl ***
...
share
Post by T0mServ0
the same common header file MySingleton.h.
The problem I'm seeing is that if I understand templates correctly, if
each
Post by T0mServ0
separate module shares the same header file, there will be multiple
instances of the MYSINGLETON created due to the templates.
I have a printf() statement in the constructor of MySingleton() which
indeed
Post by T0mServ0
tells me that more than one instance of MySingleton is created when I run
the test program. The instance in just the test module is created first,
and when libtest2.so uses it, a new instance is created there too.
Now, is there anything or compiler flag I need to be using to prevent
this?
...
Chair
Post by T0mServ0
Post by Douglas C. Schmidt
Electrical Engineering and Computer Science TEL: (615) 343-8197
www.dre.vanderbilt.edu/~schmidt
***@vanderbilt.edu
Loading...