0

让我们稍微谈谈理论。我们有一个容器,我们称它为 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)。

问题:如果最后一个函数中没有关键部分,它会是线程安全的吗?

4

1 回答 1

0

鉴于您的示例代码,它似乎对于read是线程安全的。这是假设entries[]是一个固定的大小。如果您只是简单地迭代固定集合,则没有理由修改集合的大小,因此可以进行线程安全读取

我唯一能看到的是结果可能已经过时了。问题来自于调用GetUpdatedObj--在生命周期中 Thread A可能看不到更新entries[0]

for(int i=0; i<Length(entrues); i++)
      if(entries[i].bUpdated) then return entries[i];

如果Thread B出现并更新entries[0],而i > 0这一切都取决于这是否被认为是可以接受的。

于 2011-08-13T00:27:27.477 回答