Re: making a threadsafe localtime
Quote:
Originally Posted by kdv
Both mutexes and critical sections will slow down your code.
[Responding here in case other folks see this and don't read your later responses] Sure, synchronization objects take time to execute, but having reliable, robust code that may be slower is preferable to faster code that is susceptible to corruption.
Re: making a threadsafe localtime
Hi i recently had the same problem and came up with this solution ..
// Global monitor object for the localtime function
CCriticalSection oLockLocaltime;
void MPSLocaltime(const time_t& ttTime,struct tm* ptrTM)
{
if(! ptrTM)
return;
CSingleLock oLock(&oLockLocaltime);
oLock.Lock();
struct tm* ptrTmLoc;
ptrTmLoc = localtime(&ttTime);
memcpy(ptrTm,ptrTmLoc,sizeof(struct tm));
oLock.Unlock();
}
This should do the trick ..
I didn't used CCriticalSection but some lock object for linux i wrote. I is based on the pthread lib but it basically works exactly like the CCriticalSeection/CSingle lock combo under Win32. I know because when i compile the code inside windows i simply change the base class to CCriticalSection/CSingleLock.
Greetings ..