472,119 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Thread safe way to initialize static object

Hi I have a THREAD class that uses the static variable NextThreadID to
store the id of the next thread to be created and a static Mutex to
protect it.

class THREAD {

public:
int Start(void);
private:
unsigned int ThreadID;

// Will be coppied into ThreadID when a new thread is started
and then incremented by1
static unsigned int NextThreadID;
static MUTEX Mutex;
};

int THREAD::Start(void) {
// Start a new thread

Mutex.Acquire();
ThreadID = NextThreadID;
NextThreadID++;
Mutex.Release();
if (ThreadStarted)
{
return 1;
}
else
{
return 0;
}

}

At the moment I initialize NextThreadID and Mutex before main() and set
it equal to zero. Is there a cleaner way to initalize these objects?
Obviously I cant do it in the THREAD object itself because several
threads may try to initalize the same object at the same time.

I have read
http://www-d0.fnal.gov/KAI/doc/tutor...alization.html
which kind of explains what I am trying to.
extern unsigned int THREAD::NextThreadID;
extern MUTEX THREAD::Mutex;
class nifty_counter {
static int count;
public:
nifty_counter()
{
if (count++ == 0) {
// initialize the NextThreadID and its protector
Mutex
THREAD::NextThreadID = 0;
THREAD::Mutex;
}
}
~nifty_counter()
{
if (--count == 0) {
// clean up obj1 ... objn
}
}
};

//Now every file that includes the library header also creates
a nifty_counter object and initializes it with the effect of increasing
nifty_counter
static nifty_counter nifty;

However even if i declare the nifty class a friend of the THREAD class
I get the compile time error that THREAD::Mutex and
THREAD::NextThreadID member cannot be declared here.

May 14 '06 #1
1 4074
philwozza wrote:
Hi I have a THREAD class that uses the static variable NextThreadID to
store the id of the next thread to be created and a static Mutex to
protect it.


This question is really not related to threads. It is simply about
static initialization. You need to create a separare cpp file that
contains the static definitions. e.g.

//header file
struct foo
{
static int bar;
};

//cpp file
int foo::bar = 2; // initialization by constant expression

This kind of "POD" initialization happens before the code starts execution.

This type of static initialization is alot trickier.

int foo( int initializer )
{
static int var = initializer;
return var ++;
}

Some compilers will provide a thread-safe static initialization (gcc4.x)
and other compilers may initialize var more than once because of a race
condition in the generated code for the static initialization.
May 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Steve | last post: by
15 posts views Thread by Mountain Bikn' Guy | last post: by
7 posts views Thread by Chad Zalkin | last post: by
8 posts views Thread by Senna | last post: by
13 posts views Thread by arun.darra | last post: by
reply views Thread by Anthony Williams | last post: by

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.