Bloomberg LP Interview Question
Financial Software Developerswhat about a global static? do u just declare it as a synchronized variable? is there even such thing called synchronized variable in c++?
global static is like that nobody can change it rather i can be used by many.whereas synchronised method makes variable to be accessed by only one computation at a time like semaphores in operating system,so tha data remains protected. no i dnt think that such methods are there in C++;
thanks~
i have forgotten lots of c++ specifics... have to pick up the slack
anything declared globally is has the lifetime of the program; declaring a global variable static means it only internal linkage and cannot be accessed from the same translation unit
if ever something global needs to be changed safely, maybe choose singleton pattern with proper multi-threading safety?
thanks~
i have forgotten lots of c++ specifics... have to pick up the slack
anything declared globally is has the lifetime of the program; declaring a global variable static means it only internal linkage and cannot be accessed from the same translation unit
if ever something global needs to be changed safely, maybe choose singleton pattern with proper multi-threading safety?
yes. you can have singleton like pattern for providing one point access and make sure that you use mutex or any other synchronization object for providing multithreading safety.
Something like that:
OurSingleton* GetInstance()
{
if (pInstance == NULL)
{
EnterCriticalSection(&cs);
if (pInstance == NULL)
{
try
{
pInstance = new OurSingleton();
}
catch(...)
{
// do something
}
}
LeaveCriticalSection(&cs);
}
return pInstance;
}
static data can be protected by using it inside a synchronized method.here at a time only one can use the data since locks are put on it exactly like mutex in semaphores.
- saumils1987 August 25, 2010