Sign in to follow this  

Local Static Initialization

This topic is 4076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is the following safe?
int GetNextID()
{
  static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  static int id = 0;

  pthread_mutex_lock(&mutex);
  int tempID = id++;
  pthread_mutex_unlock(&mutex);

  return tempID;
}
This is really more of a theoretical question that comes down to when local statics are initialized and if the are always ATOMIC operations. Would it still be safe if 'id' was a class? Are these really initialized the first time they are called, or at the program startup? If they are initialized the first time they are called instead of at startup and multiple threads call the function at the same time, could there race conditions? Thanks

Share this post


Link to post
Share on other sites
It isn't safe at all, in the context of what you probably want "safe" to mean.

Quote:

This is really more of a theoretical question that comes down to when local statics are initialized...Are these really initialized the first time they are called, or at the program startup?

At the latest, a local static will be initialized the first time control passes through its declaration or before its block is first entered (in the case of a POD type initialized with a constant expression).

It is allowed, however, for the initialization to be performed earlier under the same conditions that an initialization for a namespace-scoped static object is performed.

In other words, it depends.

Quote:

and if the are always ATOMIC operations.

While there may be some obscure clause regarding this in some section of the standard I'm not really familiar with, as far I know (and I'd bet good money on it) no operation is guaranteed to be atomic by the C++ language itself. Certainly there are no guarantees on the initialization of objects. You need to use processor- or OS-level methods to ensure you won't be context-switched during an operation.

Quote:

If they are initialized the first time they are called instead of at startup and multiple threads call the function at the same time, could there race conditions?

Yes. Perhaps your code handles them; I know nothing of the API you're using.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
It isn't safe at all, in the context of what you probably want "safe" to mean.

By "safe" I meant the statics would be initialized correctly and there would be no race conditions. But you are saying there would be a race condition?

Share this post


Link to post
Share on other sites
So, would this be a safer solution?
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int GetNextID()
{
pthread_mutex_lock(&mutex);

static int id = 0;
int tempID = id++;

pthread_mutex_unlock(&mutex);

return tempID;
}

Share this post


Link to post
Share on other sites
Assume absolutely nothing is atomic in C++ unless you are given an obvious and explicit guarantee otherwise. After all, even just incrementing a single integer might require copying a memory location to a register, incrementing the register, and copying back to memory, in some cases.

This may be useful reading: "C++ scoped static initialization is not thread-safe, on purpose!"

EDIT: Just seen your last post. That looks safe to me.

Share this post


Link to post
Share on other sites

This topic is 4076 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this