让我们稍微谈谈理论。我们有一个容器,我们称它为 TMyObj,如下所示:
struct TMyObj{
bool bUpdated;
bool bUnderUpdate;
}
让一个名为 TMyClass 的类有一个上面的容器数组 + 3 个有用的函数。一种用于获取要更新的对象。一种用于向某个对象添加更新信息,一种用于获取更新的对象。它也按此顺序调用。这是课程
class TMyClass{
TmyObj entries[];
TMyObj GetObjToUpdate;
{
//Enter critical section
for(int i=0; i<Length(entries); i++)
if(!entries[i].bUnderUpdate)
{
entries[i].bUnderUpdate=true;
return entries[i];
}
//Leave critical section
}
//the parameter here is always contained in the Entries array above
void AddUpdateInfo(TMyObj obj)
{
//Do something...
//Enter critical section
if(updateInfoOver) obj.bUpdated=true; //left bUnderUpdate as true so it doesn't bother us
//Leave critical section
}
TmyObj GetUpdatedObj
{
//<-------- here
for(int i=0; i<Length(entrues); i++)
if(entries[i].bUpdated) then return entries[i];
//<-------- and here?
}
}
现在想象 5+ 个线程使用前两个线程,另一个线程在上面类的一个实例上使用最后一个函数 (getUpdadtedObj)。
问题:如果最后一个函数中没有关键部分,它会是线程安全的吗?