| View previous topic :: View next topic |
| Author |
Message |
Jeremy Godfrey Guest
|
Posted: Wed Sep 24, 2003 11:41 am Post subject: Singleton in a multithreaded app |
|
|
Hi,
"Modern C++ Design" talks about safely creating singletons in a
multithreaded app by using a mutex object but seems to overlook how to
safely create the mutex object. What thoughts do people have on this? Is
boost::call_once() the best way to safely create the mutex in a portable
way?
Jeremy Godfrey
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alexander Terekhov Guest
|
Posted: Wed Sep 24, 2003 10:28 pm Post subject: Re: Singleton in a multithreaded app |
|
|
Jeremy Godfrey wrote:
| Quote: |
Hi,
"Modern C++ Design" talks about safely creating singletons in a
multithreaded app by using a mutex object but seems to overlook how to
safely create the mutex object. What thoughts do people have on this? Is
boost::call_once() the best way to safely create the mutex in a portable
way?
|
Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
regards,
alexander.
--
typedef std::aligned_storage<std::mutex> pthread_mutex_t;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Andrei Alexandrescu (See Guest
|
Posted: Wed Sep 24, 2003 10:42 pm Post subject: Re: Singleton in a multithreaded app |
|
|
"Jeremy Godfrey" <nospam (AT) nospam (DOT) com> wrote
| Quote: | Hi,
"Modern C++ Design" talks about safely creating singletons in a
multithreaded app by using a mutex object but seems to overlook how to
safely create the mutex object. What thoughts do people have on this? Is
boost::call_once() the best way to safely create the mutex in a portable
way?
|
A once function is a good way to go. Absent that, you can rely on an atomic
integer.
Andrei
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Seigh Guest
|
Posted: Thu Sep 25, 2003 9:19 pm Post subject: Re: Singleton in a multithreaded app |
|
|
Alexander Terekhov wrote:
| Quote: |
Jeremy Godfrey wrote:
Hi,
"Modern C++ Design" talks about safely creating singletons in a
multithreaded app by using a mutex object but seems to overlook how to
safely create the mutex object. What thoughts do people have on this? Is
boost::call_once() the best way to safely create the mutex in a portable
way?
Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
|
Statically intialized in C, correct? I assume you can't assume
that static inialization of C++ objects will work correctly or
that there is even such a thing?
Joe Seigh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
WW Guest
|
Posted: Fri Sep 26, 2003 10:35 am Post subject: Re: Singleton in a multithreaded app |
|
|
Joe Seigh wrote:
| Quote: | Alexander Terekhov wrote:
[SNIP]
Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
Statically intialized in C, correct? I assume you can't assume
that static inialization of C++ objects will work correctly or
that there is even such a thing?
|
"pthread_mutex_t POD" he says and "magic" "PTHREAD_MUTEX_INITIALIZER" he
says. AFAIK static initialization of PODs works the same way in C++ as in
C. Please correct me if I am wrong. And please do it so by standard
quotes.
--
WW aka Attila
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alexander Terekhov Guest
|
Posted: Fri Sep 26, 2003 10:37 am Post subject: Re: Singleton in a multithreaded app |
|
|
Joe Seigh wrote:
[...]
| Quote: | Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
Statically intialized in C, correct? I assume you can't assume
that static inialization of C++ objects will work correctly or
that there is even such a thing?
|
typedef std::aligned_storage<std::mutex> pthread_mutex_t; // POD
#define PTHREAD_MUTEX_INITIALIZER { /* magic */ }
regards,
alexander.
P.S. std::pthread_mutex_t * mutex::c_mutex() throw();
--
extern "C" int pthread_mutex_destroy(pthread_mutex_t * m) throw() {
m->object().~object();
// "may fail" shall be caught in the std::unexpected() handler
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Seigh Guest
|
Posted: Sat Sep 27, 2003 1:47 am Post subject: Re: Singleton in a multithreaded app |
|
|
WW wrote:
| Quote: |
Joe Seigh wrote:
Alexander Terekhov wrote:
[SNIP]
Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
Statically intialized in C, correct? I assume you can't assume
that static inialization of C++ objects will work correctly or
that there is even such a thing?
"pthread_mutex_t POD" he says and "magic" "PTHREAD_MUTEX_INITIALIZER" he
says. AFAIK static initialization of PODs works the same way in C++ as in
C. Please correct me if I am wrong. And please do it so by standard
quotes.
|
Sorry, I don't have a copy of the standard. That's why I was asking the
question.
In C++, if you had
class Something {
int x;
int y;
public:
Something() : x(0), y(0) {}
};
then
static Something z;
would initialize the same as doing the following in C
struct something_t {
int x;
int y;
};
static struct something_t z = {0, 0};
For thread safety, you want static initialization done before "main" is called
when there is supposedly only one thread running. I don't know if POD
initializers in C++ ctors have to work that way.
It may work in some implementations but since C++ says nothing with respect to
threads, you may have to imbed some stuff in C just on general principle.
Joe Seigh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Sat Sep 27, 2003 10:16 am Post subject: Re: Singleton in a multithreaded app |
|
|
Joe Seigh <jseigh_01 (AT) xemaps (DOT) com> wrote
| Quote: | Alexander Terekhov wrote:
Jeremy Godfrey wrote:
"Modern C++ Design" talks about safely creating singletons in a
multithreaded app by using a mutex object but seems to overlook
how to safely create the mutex object. What thoughts do people
have on this? Is boost::call_once() the best way to safely create
the mutex in a portable way?
Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
Statically intialized in C, correct? I assume you can't assume that
static inialization of C++ objects will work correctly or that there
is even such a thing?
|
Static initialization in C++ works just like it does in C. And
pthread_mutex_t is a C struct. C compatibility in C++ is there for a
purpose.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sat Sep 27, 2003 2:45 pm Post subject: Re: Singleton in a multithreaded app |
|
|
In article <3F742BB7.647F74A4 (AT) xemaps (DOT) com>, Joe Seigh
<jseigh_01 (AT) xemaps (DOT) com> writes
| Quote: | Sorry, I don't have a copy of the standard. That's why I was asking the
question.
In C++, if you had
class Something {
int x;
int y;
public:
Something() : x(0), y(0) {}
};
then
static Something z;
would initialize the same as doing the following in C
struct something_t {
int x;
int y;
};
static struct something_t z = {0, 0};
For thread safety, you want static initialization done before "main" is called
when there is supposedly only one thread running. I don't know if POD
initializers in C++ ctors have to work that way.
|
They do, but as you provide a ctor something_t will be statically zero
initialised (much as for C, and in this case exactly as for C) and
dynamically initialised by calling the ctor. The latter, might be
optimised away in this case).
| Quote: |
It may work in some implementations but since C++ says nothing with respect to
threads, you may have to imbed some stuff in C just on general principle.
|
But C says nothing about threads either. Globals must be ctored before
their point of first use (and before any function in the same TU is
used.)
--
Francis Glassborow ACCU
If you are not using up-to-date virus protection you should not be reading
this. Viruses do not just hurt the infected but the hole community.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Raoul Gough Guest
|
Posted: Mon Sep 29, 2003 9:33 pm Post subject: Re: Singleton in a multithreaded app |
|
|
Alexander Terekhov <terekhov (AT) web (DOT) de> writes:
| Quote: | Jeremy Godfrey wrote:
Hi,
"Modern C++ Design" talks about safely creating singletons in a
multithreaded app by using a mutex object but seems to overlook how to
safely create the mutex object. What thoughts do people have on this? Is
boost::call_once() the best way to safely create the mutex in a portable
way?
Don't use boost::call_once() for that (its sort of "funny" impl and
its applicability for other things aside for a moment). You can use
pthread_mutex_t POD and the magic of PTHREAD_MUTEX_INITIALIZER to
initialize statically allocated mutex.
|
That won't be as portable as boost::call_once, though. Do you think
call_once is actually wrong for managing singletons (or even just
singleton mutexes)?
--
Raoul Gough.
(setq dabbrev-case-fold-search nil)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|