By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,369 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,369 IT Pros & Developers. It's quick & easy.

how to avoid static pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER;

P: n/a
Sorry if I am OT, It seems I cannot declare a per object ncwrite_lock
in a C++ class.

class C{
pthread_mutex_t ncwrite_lock;
C () : ncwrite_lock(PTHREAD_MUTEX_INITIALIZER){} // error
};

class C{
pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER; // error
C () {}
};

class C{
pthread_mutex_t ncwrite_lock;
C () { ncwrite_lock = PTHREAD_MUTEX_INITIALIZER; // error }
};

class C{

C () {}
void method(){
static pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER; //
ok
}
};

Is there a way not to declare a global scope pthread_mutex_t and still
be able to use the lock across methods within a single class?

Thanks!

Apr 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Fei Liu wrote:
Sorry if I am OT, It seems I cannot declare a per object ncwrite_lock
in a C++ class.

[snip]

Yes, it does look like your are OT here. You want to try looking in
a news group that is related to your specific compiler/platform.
Threads and locks are handled quite differently on different platforms.

Start with groups.google.com and search the news groups for one
dedicated to your compiler, or your operating system. Also
search for groups that talk about the keywords you have used.
It's quite possible the answer to your question is already on line.
Socks

Apr 10 '06 #2

P: n/a
Fei Liu wrote:
Sorry if I am OT, It seems I cannot declare a per object ncwrite_lock
in a C++ class.

class C{
pthread_mutex_t ncwrite_lock;
C () : ncwrite_lock(PTHREAD_MUTEX_INITIALIZER){} // error
};

class C{
pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER; // error
C () {}
};

class C{
pthread_mutex_t ncwrite_lock;
C () { ncwrite_lock = PTHREAD_MUTEX_INITIALIZER; // error }
};

class C{

C () {}
void method(){
static pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER; //
ok
}
};

Is there a way not to declare a global scope pthread_mutex_t and still
be able to use the lock across methods within a single class?

Thanks!


This really belongs in comp.os.linux.development.apps, but...

PTHREAD_MUTEX_INITIALIZER can only be used to init a 'static'
or global mutex, eg:

static pthread_mutex_t muxA = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t muxB = PTHREAD_MUTEX_INITIALIZER;

/* pthread_mutex_init( &mux, NULL ) init's a non-static/non-global
* mutex to the same defaults as PTHREAD_MUTEX_INITIALIZER.
*/
class C
{
pthread_mutex_t ncwrite_lock;
C () { pthread_mutex_init( &ncwrite_lock, NULL ); }
};

Regards,
Larry
Apr 10 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.