Link to home
Start Free TrialLog in
Avatar of thready
thready

asked on

vector and threads

Hi Experts,

I want to have a bunch of threads that share a vector.  If I keep a critical section around the vector at all places where it is accessed, am I ok?

i.e., I place a critical section around the size() call where the parent class that owns the vector is a GetSize

I place a critical section around code that adds items to the vector in the AddItem function

Is there anything else to watch out for?

Thanks,
Mike
Avatar of thready
thready

ASKER

Also, my concern is here:


int TaskRunner::GetNumberOfTasksInQueue()
{
      EnterCriticalSection(&m_QueueCriticalSection);

      int nSize = m_TaskQueue.size();

      LeaveCriticalSection(&m_QueueCriticalSection);

     // RIGHT HERE - SIZE MIGHT BE INCORRECT - ANOTHER THREAD MAY HAVE COME IN AND CHANGED SIZE
//  JUST BEFORE I GOT TO RETURN IT.  HOW DO I FIX THIS?

      return nSize;
}
Avatar of thready

ASKER

ignore that last concern - I realized that size is a stack variable - not a member variable like I was thinking....
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of thready

ASKER

Gracias amigo