473,387 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to avoid static pthread_mutex_t ncwrite_lock = PTHREAD_MUTEX_INITIALIZER;

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
2 3808
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
2
by: | last post by:
I have this class: --------------------------------------------- class A { string a; int instances; A(); static vector<A> all; public: static A *CreateA(...................);
9
by: Andrew Au | last post by:
Dear all, I am trying to write a piece of software that use Object Oriented design and implement it with C, I did the following == In Object.h == typedef struct ObjectStructure* Object; ...
4
by: Roland Zerek | last post by:
Hi, Is there any way to avoid the compiler/linker to optimize one (and only this) class member? Below is the code: <code> class MyFactory { /* . . . */ public static void Register(...
4
by: Raman | last post by:
Hi All, The declaration of pthread_mutex_init in pthread.h is as in pthread.h Some one please explain. ( what is "__resrict", whay "__" before "mutex" and what is __THROW. extern int...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
18
by: StephQ | last post by:
I'm facing the following problem: I'm using the Gnu Scientific Library (it is a C math library), and in particular random number generation algorithms. Example code is: .... gsl_rng * r; .......
6
by: howa | last post by:
Since it is part fo the standard, why I always heard that we should avoid iframe? any comments? thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.