1
当谈到关于创建单例实例中的竞争条件的单例和线程安全问题时,我们在讨论哪个线程?单例和线程安全
以此为例子,假设我有一个使用一个Singleton
class MyApp
{
MySingleton oneAndOnly;
int main() // application entry point
{
oneAndOnly = MySingleton::GetInstance();
}
void SpawnThreads()
{
for(int i = 0; i < 100; i++)
{
Thread spawn = new Thread(new ThreadStart(JustDoIt));
spawn.Start();
}
}
void JustDoIt()
{
WaitRandomAmountOfTime(); // Wait to induce race condition (maybe?) for next line.
MySingleton localInstance = MySingleton::GetInstance();
localInstance.DoSomething();
}
}
一个MyApp的是它在谈论:
- 当我打开MyApp.exe的一次, 再一次再次尝试让 都打开?
- 还是说到MyApp产生的线程?如果MyApp确实 未产生线程会怎么样?