Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

vector and threads

Posted on 2015-02-18
4
Medium Priority
?
129 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

618 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