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

  • Home
  • SEARCH
  • 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 9105743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:10:33+00:00 2026-06-17T02:10:33+00:00

Consider the following situation: I have an object foo that is used by multiple

  • 0

Consider the following situation:

I have an object foo that is used by multiple threads, that may or may not repeatedly call a method bar() on foo.

It is perfectly fine (and desired) that bar() is executed multiple times in parallel, as it never changes the state of foo.

The problem arises when i need to change the state of foo from the outside (from another thread, not from one of the “worker” threads) – how can i lock foo in a way so that the calling thread blocks until the last worker thread is done with bar() and all worker threads will block at bar() until i release foo again?

Obviously i cannot just use a mutex that is remains locked during execution of bar(), because then i won’t have concurrency there.

Any ideas? Or is there a better design for those types of problems?

  • 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-06-17T02:10:34+00:00Added an answer on June 17, 2026 at 2:10 am

    I am not sure how you are going to achieve, that none of workers are using foo to let writer update it, but if it is not of a concern then just use a read/write mutex (workers to obtain read lock, writer to obtain write lock).

    It is worth mentioning though, that you might want to consider making foo Copy-on-Write. This way you will make synchronization overhead close to zero. You can use shared_ptr atomically to achieve that.

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

Sidebar

Related Questions

Consider the following situation: I have a library that includes a third party library.
Consider the following situation: We have two Localizable.string files, one in en.lproj and one
Consider the following situation: I have two android projects named P1 and P2 which
I have an app that's about presenting fictional simplified cities. Please consider the following
Consider following situation: Object > MyClass > MyClassA, MyClassB If I want something in
Consider the following common situation: You have some MainView in your Cocoa application, loaded
Consider a situation where you have an object in a storyboard file linked up
consider the following situation: I have a string s and hash function H generating
consider the following situation: In order to have lots of work processed at my
Consider the following situation. I have 3 tables in a resource planning project(created using

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.