|
-
November 1st, 2010, 04:59 AM
#2
Re: Thread Safe Singleton Segmentation Fault
In GetInstance you access m_DbManager unsynchronized, so to make it thread safe, you should change it to.
 Originally Posted by Peter_APIIT
Code:
ClsSqliteDatabaseManager* ClsSqliteDatabaseManager::GetInstance()
{
unique_lock<mutex> lock(m_Mutex);
while (m_DbManager == 0)
{
m_CondVar.wait(lock);
}
return m_DbManager;
}
However, that still leaves the possibility of a deadlock if CreateInstance is not called. IMO this design is difficult to use correctly.
It's not clear to me how DeleteInstance should be used. Why does it take a parameter?
Also, if you want to explicitly create and delete your singleton, why not create it before any worker threads are created and delete it after all worker threads have finished. That way you don't need any synchronization, at least not for accessing the singleton.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|