how to avoid static pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER;

F

Fei Liu

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!
 
P

Puppet_Sock

Fei said:
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
 
L

Larry I Smith

Fei said:
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
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top