ID generator with local static variable - thread-safe?

Posted by Poseidon on Stack Overflow See other posts from Stack Overflow or by Poseidon
Published on 2010-04-24T18:35:24Z Indexed on 2010/04/24 18:43 UTC
Read the original article Hit count: 152

Filed under:
|
|

Will the following piece of code work as expected in a multi-threaded scenario?

int getUniqueID()
{
static int ID=0;
return ++ID;
}

It's not necessary that the IDs to be contiguous - even if it skips a value, it's fine. Can it be said that when this function returns, the value returned will be unique across all threads?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c