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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:04:26+00:00 2026-05-15T17:04:26+00:00

My problem is, I have block matrix updated by multiple threads. Multiple threads may

  • 0

My problem is, I have block matrix updated by multiple threads.
Multiple threads may be updating disjoint block at a time but in general there may be race conditions. right now matrix is locked using single lock.

The question is, is it possible (and if it is, how?)
to implement an efficient array of locks, so that only portions of matrix maybe locked at a time.

The matrix in question can get rather large, on order of 50^2 blocks. my initial guess is to use dynamically allocate vector/map of mutexes.

Is it good approach?
Is it better to use multiple condition variables instead?
Is there a better approach?

  • 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-15T17:04:27+00:00Added an answer on May 15, 2026 at 5:04 pm

    Use a single lock. But instead of using it to protect the entire matrix use it to guard a std::set (or a boost::unordered_set) which says which blocks are “locked”.

    Something like this.

    class Block;
    
    class Lock_block
    {
    public:
       Lock_block( Block& block ) : m_block(&block)
       {
          boost::unique_lock<boost::mutex> lock(s_mutex);
          while( s_locked.find(m_block) != s_locked.end() )
          {
             s_cond.wait(lock);
          }
          bool success = s_locked.insert(m_block).second;
          assert(success);
       }
    
       ~Lock_block()
       {
          boost::lock_guard<boost::mutex> lock(s_mutex);
          std::size_t removed = s_locked.erase(m_block);
          assert(removed == 1);
          s_cond.notify_all();
       }
    private:
       Block* m_block;
    
       static boost::mutex s_mutex;
       static boost::condition s_cond;
       static std::set<Block*> s_locked;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom view, that has page and block. But the problem when
I have a problem with position of a block (div). I am newbie in
Problem: I have a table that prints out vertical but I would like it
So the problem is when you have a block of text, and an image
I have a general problem where I have an array of function pointers. The
I'm having a hard time working out the following problem efficiently . I have
I have problem with the else block. The if block works well - when
The problem: we have critical path code that should never block. In many places
Problem: Have made a small mail program which works perfectly on my developer pc
I've seen that a few instances of this problem have been raised already. However,

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.