Solved

vector and threads

Posted on 2015-02-18
4
126 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

749 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