Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7513307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:08:35+00:00 2026-05-30T00:08:35+00:00

One class XYZ has 3 member variable x, y, z. There is vector of

  • 0

One class XYZ has 3 member variable x, y, z. There is vector of [N] XYZ objects.

There are 3 threads A, B, C, they can access any objects in the vector and any member variable of that object.

class XYZ
{
public:
double x;
double y;
double z;
};

std::vector with N elements, where N is fixed throughout the program.

How to design the inter-thread communication to achieve thread-safety and maximum efficiency, i.e., minimum blocking.

Here is some of my thinking process, please correct me if I am wrong.

  1. Divide vector in smaller vectors and encapsulate into each thread
    class, then use a message queue to pass data around. The problems
    are all 3 threads can access anywhere of the vector and objects
    member, therefore it is difficult to sub-divide and encapsulate.
    Message queue itself needs blocking, i.e., reader needs to be
    blocked when sender is adding to the queue.
  2. Use atomic library to make access atomic, therefore avoiding
    blocking. The problem is atomic is OS dependent, i.e., some
    operation is considered atomic under Linux may not be atomic under
    Windows.
  3. Mutex, add 3 mutex objects for each member variable, e.g., mutex_x,
    mutex_y, mutex_z. However, the problem is that mutex is noncopyable,

i.e., if we have a class like,

class XYZ_mutex
{
public:
double x;
double y;
double z;
boost::mutex mutex_x;
boost::mutex mutex_y;
boost::mutex mutex_z;
};

We can NOT have a vector of XYZ_mutex, because .push_back() is a copy constructor.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:08:37+00:00Added an answer on May 30, 2026 at 12:08 am

    You need to consider what usage patterns and what consistency requirements your program has.

    First and most important usage pattern is whether any of the threads needs to modify these structures. If not, you don’t actually need any locking – just make sure the structure is populated before the threads start reading it.

    If the threads do have to modify the structure, you need to consider consistency. You need to ask yourself, if there are any constraints, other than modifying an individual double value, that limit when one thread can see changes made by another thread.

    Once you define that, start thinking if data can be split between threads in any way to reduce conflict between threads – even if it can’t be completely eliminated, the answer the question whether it’s better to keep three vectors of doubles, a vector of XYZ or some other organisation depenst on whether there will be some threads that access certain XYZ objects more often, or if there will be threads that access x members more often, while others access ys or zs more often etc.

    If you can’t say anything about the probability of accessing any member of any instance by any thread, it’s hard to say how to best organise them. It might even be a good idea to place them on the heap so that they land in separate cache lines.

    Overall, probably the best suggestion is to start by putting them in any data structure that’s best suited for your needs (vector, map, set etc.), using a single mutex to synchronise the whole thing, writing your program and then testing if you experience a bottleneck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one class that has a list of objects of Daemon type. class
Let's say I have one class Foo that has a bunch of logic in
There's a LinkedQueueClass that has 3 elements: ABC XYZ 123 The elements inside the
I used to have one class for one file. For example car.cs has the
I have one class that needs to grab an attribute that is set in
Let's say you've inherited a C# codebase that uses one class with 200 static
I have a namespace ABC, which has one form class, F and an ordinary
I have a XYZ class in that I have written two methods A and
I am implementing Factory Pattern in php. There is an AbstractFactory Class and one
In one class I have a library project with a file class1 with namespace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.