Solved

vector and threads

Posted on 2015-02-18
4
120 Views
Last Modified: 2015-02-18
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
0
Comment
Question by:thready
  • 3
4 Comments
 
LVL 1

Author Comment

by:thready
Comment Utility
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;
}
0
 
LVL 1

Author Comment

by:thready
Comment Utility
ignore that last concern - I realized that size is a stack variable - not a member variable like I was thinking....
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
>> If I keep a critical section around the vector at all places where it is accessed, am I ok?

Short ad sweet: if 'accessed' means both read and write access, you are absolutely fine.
0
 
LVL 1

Author Closing Comment

by:thready
Comment Utility
Gracias amigo
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now