Compounds in Init lists

G

gwowen

I wrote a simple wrapper class around pthreads to implement a
producer/consumer thread pool. A simplified form resembled this:

#include <pthread.h>
class C
{
pthread_cond_t m_cond;
}

// with constructor
C::C ()
: m_cond(PTHREAD_COND_INTIALIZER)
{}

On win32 g++/mingw, this compiled beautifully, but on Linux, it
didn't.
This is because the typedefs differ.

On Linux, pthread_cond_t is a struct, and the macro PTHREAD_COND_T is
(something like) {0,0,NULL,0}, which can't be used in an
initialization list.
On Win32 pthread_cond_t is a pointer to an incomplete type.

Local coding standards say I should initialize the compound in the
initialization list.

Is this possible without doing, say the following:

namespace {
static const pthread_cond_t
pthread_cond_intializer = PTHREAD_COND_INTIALIZER;
}

C::C ()
: m_cond(pthread_cond_intializer)
{}

Will it be possible in C++0x?

(Yes, I know boost::thread...
It was an exercise, more than anything else.)
 
J

James Kanze

I wrote a simple wrapper class around pthreads to implement a
producer/consumer thread pool. A simplified form resembled
this:
#include <pthread.h>
class C
{
pthread_cond_t m_cond;
}
// with constructor
C::C ()
: m_cond(PTHREAD_COND_INTIALIZER)
{}

This isn't really legal.
On win32 g++/mingw, this compiled beautifully, but on Linux,
it didn't. This is because the typedefs differ.
On Linux, pthread_cond_t is a struct, and the macro
PTHREAD_COND_T is (something like) {0,0,NULL,0}, which can't
be used in an initialization list.

On most systems I know, pthread_cond_t will be a struct.
On Win32 pthread_cond_t is a pointer to an incomplete type.
Local coding standards say I should initialize the compound in
the initialization list.

Formally, you can't initialize a pthread_cond_t in an
initialization list. Regardless of what local coding standards
say. This is often an issue when interfacing with C or with
APIs defined in terms of C. The API uses struct's, and you
can't initialize a struct in an initialization list. In Posix,
unless the specification says explicitly that the type is not a
struct, you must suppose that it might be a struct. And some
types are required to be structs: how would you initialize a
struct tm or a struct stat in an initialization list?
Is this possible without doing, say the following:
namespace {
static const pthread_cond_t
pthread_cond_intializer = PTHREAD_COND_INTIALIZER;
}
C::C ()
: m_cond(pthread_cond_intializer)
{}

That will almost certainly get the code through the compiler,
and in practice, I can't imagine it not working. But it's not
legal Posix---you're not allowed to copy pthread_cont_t.
Will it be possible in C++0x?

I don't think so.
 
C

Chris M. Thomasson

gwowen said:
I wrote a simple wrapper class around pthreads to implement a
producer/consumer thread pool. A simplified form resembled this:

#include <pthread.h>
class C
{
pthread_cond_t m_cond;
}

// with constructor
C::C ()
: m_cond(PTHREAD_COND_INTIALIZER)
{}
[...]

In addition to James, you cannot legally initialize a `pthread_cond_t' that
does not have static storage duration with `PTHREAD_COND_INTIALIZER'. This
is not legal:


pthread_cond_t cond = PTHREAD_COND_INTIALIZER;


This is also not legal:


static pthread_cond_t g_cond = PTHREAD_COND_INTIALIZER;

pthread_cond_t l_cond = g_cond;
 
J

James Kanze

"gwowen" <[email protected]> wrote in message

[...]
This is also not legal:
static pthread_cond_t g_cond = PTHREAD_COND_INTIALIZER;
pthread_cond_t l_cond = g_cond;

As I said in my posting, pthread_cond_t is not copiable.

I'm pretty sure that this is the intent; I know that
pthread_mutex_t, at least under Solaris and Linux, contain
immediate data, and that using copies will not work. And in OO
parlance, the types have identity, so you don't expect copies to
work. On the other hand, I can't find off hand where Posix says
this (probably because copying makes so little sense for these
types, it didn't occur to them to explicitly forbid it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top