After thinking about it, the plot of the source code can be roughly represented by the following code
#include
#include
#include
#include
#include
#include
using namespace std;
struct World;
struct Person
{
std::vector
std::string m_Name;
World *m_pWorld = nullptr;
Person(const std::string &name)
: m_Name(name)
{}
};
int g_TrainThreadInited = 0;
int g_TrainThreadCrashed = 0;
int g_TryTimes = 0;
Person g_p1("Stevens"), g_p2("Sean");
struct World
{
Person *m_pMainCharacter = nullptr;
World(Person *character)
: m_pMainCharacter(character)
{}
void SadStory()
{
cout << "Boom !" << endl;
}
void HappyStory()
{
cout << "Mua !" << endl;
}
};
void TrainThread(World *pWorld)
{
try
{
if (g_TryTimes <8)
{
pWorld->SadStory();
++g_TryTimes;
throw std::exception("Boom !");
}
else
pWorld->HappyStory();
}
catch (std::exception &e)
{
pWorld->m_pMainCharacter->m_Memories.push_back(e.what());
g_TrainThreadCrashed = 1;
}
}
int main()
{
std::shared_ptr
for (;;)
{
if (!g_TrainThreadInited || g_TrainThreadCrashed)
{
World w(&g_p2);
g_p1.m_pWorld = &w;
w.m_pMainCharacter = &g_p1;
threadptr.reset(new thread (TrainThread, &w));
threadptr->detach();
if (!g_TrainThreadInited)
g_TrainThreadInited = 1;
if (g_TrainThreadCrashed)
g_TrainThreadCrashed = 0;
}
}
return 0;
}
View more about Source Code reviews