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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:48:24+00:00 2026-05-30T21:48:24+00:00

In a worker thread, I iterate over a list of items that need updating.

  • 0

In a worker thread, I iterate over a list of items that need updating. In the main thread, the user often calls a function to add elements to that list. I’m using a mutex to prevent the list from being modified while it’s being iterated. Generally, the pseudocode looks like this:

// called by the client to add an element.
void AddElementToList(element)
{
    // lock mutex
    // add element to the list
    // unlock mutex
}

// this function is run by the worker thread
void WorkerThreadUpdate()
{
    // lock mutex
    // for each element in the list
    //     update the element
    //     mark the element for deletion if finished
    // for every finished element
    //     remove element from list
    // unlock mutex
}

The problem comes when updating the element. The element’s update function can potentially take a long time, up to about one second in some cases. When this occurs, the user’s next call to AddElementToList blocks, causing their application to freeze until all the element have been updated. The AddElementToList function is called often enough for this to be a very noticable problem.

So I’m looking for a better method. I want to update the elements while protecting the list but keep the user’s main thread responsive. I’m sure this is pretty much multithreading 101, but I can’t come up with the correct terms to find examples of what I’m looking for.

I was thinking of a solution that uses two lists, a “request” list and a “work” list.

The user calls AddElementToList which adds the element to the request list. In the worker thread, after the work list has been iterated, it goes through the request list and adds its elements to the work list for the next frame. It only locks when it actually modifies the lists.

// called by the client to add an element.
void AddElementToList(element)
{
    // lock mutex
    // add element to the request list
    // unlock mutex
}

// this function is run by the worker thread
void WorkerThreadUpdate()
{
    // for each element in the work list
    //     update the element
    //     mark the element for deletion if finished

    // for every finished element
    //     lock mutex
    //     remove element from work list
    //     unlock mutex

    // lock mutex
    // for every element in the request list
    //     add element to the work list
    // clear the request list
    // unlock mutex
}

I think this should work, but I’m not entirely sure. Is this an acceptable way? Are there better methods for handling this?

  • 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-30T21:48:25+00:00Added an answer on May 30, 2026 at 9:48 pm

    Your plan to queue up the additions should work. Whether it’s acceptable or not depends on whether it’s acceptable to wait for queue additions until the thread does its next ‘update’ pass.

    Is it even necessary to lock the list during the update? What else is accessing this list, and when? Can you lock the list, make a copy vector/list of the references, unlock the list and then run the update on each of the the copy refs one-by-one?

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

Sidebar

Related Questions

I have a worker thread that needs to add items to a BindingList .
I have a background worker that is started on the main thread as shown
I need a way to stop a worker thread that does not contain a
In the main loop of a worker thread that has to poll to know
I have a worker thread that is listening to a TCP socket for incoming
I have a worker thread in a class that is owned by a ChildView.
I have a worker thread that may be active for short bursts of time
Our WCF services occaisionally spawn a worker thread to handle something that the client
I need to kill specific worker thread in my application. I don't have any
Sockets on Linux question I have a worker thread that is blocked on an

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.