[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

vector and threads

Posted on 2015-02-18
4
Medium Priority
?
130 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
ID: 40617226
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
ID: 40617232
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 2000 total points
ID: 40617318
>> 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
ID: 40617388
Gracias amigo
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

873 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