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 996041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:49:00+00:00 2026-05-16T06:49:00+00:00

I have one ‘main’ thread that creates an array of objects of class SlowData

  • 0

I have one ‘main’ thread that creates an array of objects of class SlowData. These objects are relatively slow to manipulate with. This is why, when I initially load them, they are filled with stub data.

Immediately after each object in the array is loaded, I can display the array in the UI. Parallel with that, I initiate the computation over the array. The SlowDataComputation object is instructed to report results to the UI (which in turn will visualize the modification of a particular SlowData object).

Here’s how this would look:

void main()
{
  std::vector<SlowData> aData;
  aData.reserve(1000000); // one million instances

  Fast_PopulateVector(aData);

  DataUi aUi;

  SlowDataComputation aCalc;
  aCalc.SetUiCallback(aUi);           // instruct the object to periodically
                                      // report computation results to the UI

  aCalc.Slow_ComputeInSeveralThreads(aData);

  aUi.VisualizeInMessageLoop(aData);  // message loop that exits upon user signal

  if ( aCalc.StillWorking() )
    aCalc.Stop();                     // terminate threads gradually

  // after VisualizeInMessageLoop returns, aData will then be destroyed
}

I want SlowDataComputation to launch several threads, each of which would process a certain segment within the array:

class SlowDataComputation
{
  std::vector<SlowData> * myData;
  DataUi                * myUI;

public:
  void Slow_ComputeInSeveralThreads(std::vector<SlowData> & theV)
  {
    myData = &theV;

    size_t aSize = myData->size();
    size_t aNumThreads = 10;
    size_t aBlockSize = aSize / aNumThreads;

    for (size_ti = 0; i < aNumThreads; i++)
    {
      ComputationThread aThr(myData);
      aThr.SetBoundary(i * aBlockSize, (i+1) * aBlockSize);
      aThr.SetUiCallback(myUI);

      aThr.Run();   // process a given segment in the array
    }
  }
};

Finally, here’s some pseudo-code for the UI:

class DataUi
{
  std::vector<SlowData> * myData;

public:
  void VisualizeInMessageLoop()
  {
    Message aMsg;

    while ( HasMessage(aMsg) )
      if (aMsg.Code == Message::MSG_PROGRAM_QUIT)
        SignalQuit();  // next time, HasMessage will return false

    // return from message loop
  }
};

The question is: how do I secure the destruction of SlowData instances so that if any of the ten computation threads is currently working with an object, destruction needs to wait until each of these computation threads completes (either because it finished processing or because the SlowDataComputation::Stop method was called)? What synchronization primitive is most suitable for this task?

I managed to solve this problem with one computation thread. I used Critical Sections. But this solution doesn’t scale well for more than one computation threads.

  • 1 1 Answer
  • 1 View
  • 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-16T06:49:01+00:00Added an answer on May 16, 2026 at 6:49 am

    At the end of your main function join all your threads with pthread_join
    Something like …

      for(t=0; t<NUM_THREADS; t++) {
         rc = pthread_join(thread[t], &status);
         //check rc and status
      }
      //destroy shared data safely. All the threads are done.
    

    you don’t need to lock the object, which is more expensive. You just need to know that all the threads finished their computation.

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

Sidebar

Related Questions

I have one data model 'object' with fields->object_id, object_name. That is: http://localhost:3000/objects/ I have
I have one class that shows me a list of Items. Right now I
Have one object of type A that is related to a bunch of objects
I have one thread that writes results into a Queue. In another thread (GUI),
I have one std::list<> container and these threads: One writer thread which adds elements
have one time consuming step that flattens a bunch of files. basically i'd like
I have one testing module that I want to use for android testing. I
i have one div that contain three div for a part in my website
I have one main form, and i want to dynamically change it's content, via
I have one solution in VS2010 containing two MVC 3 projects. These are two

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.