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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:08:22+00:00 2026-05-28T18:08:22+00:00

Pseudocode: Function1_vector_copy () { vectora = vectorb; } Function2_vector_search() { find k in vectora;

  • 0
Pseudocode:
Function1_vector_copy () {
vectora = vectorb;
}
Function2_vector_search() {
find k in vectora;
}

The program is multi-threaded. While many threads may search , vector copying is done only by a single thread but at occasional times. The problem is whatever it may, vector_search should not fail. Vector_copy can be deferred but vector_search should not be. Its a no delay, no failure pattern. The problem is the shared variable vectora which has to be persistent, so that vector_search is not failed at all. What is an optimal way in achieving this ?

Edit :

Using an answer for another question

boost::shared_mutex _access;

Function1_vector_copy() {

  // get upgradable access
  boost::upgrade_lock lock(_access);

  // get exclusive access
  boost::upgrade_to_unique_lock uniqueLock(lock);
  // now we have exclusive access

  vectora.swap(vectorb);
}

Function2_vector_search() {

  // get shared access
  boost::shared_lock lock(_access);

  // now we have shared access

  find k in vectora ;

}

If a thread with upgradable ownership tries to upgrade whilst other
threads have shared ownership, the attempt will fail and the thread
will block until exclusive ownership can be acquired.
–boost-documentation

The trick is the vector copy is done asynchronously and it will happen after exclusive ownership is acquired. So vector copy does eventually happen but deferred and indeed vector copy is also a non-failure operation but delayed which is okay for me. There will be a lock somehow for synchronization and we will block for a milli second atleast. But the advantage of using this swap will result in lower time of that making it negligible.

  • 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-28T18:08:23+00:00Added an answer on May 28, 2026 at 6:08 pm
    Function1_vector_copy () 
    {
        VectorType tmp = vectorb;
    
        rwLock.acquireWrite();
        swap(tmp,vectora);
        rwLock.releaseWrite();
    }
    

    The rest is left as an exercise for the reader.

    UPDATE: Summarizing the comments below…

    First of all I should make clear that the original swap(...) call above was intended as pseudo-code, not necessarily to be taken literally. I didn’t know exactly what VectorType was (and technically still don’t). I assumed that you would intend on doing as efficient a swap() as was possible. It was pseudo-code after all…

    In any case, std::swap() is specialized for all container types in the C++ standard library. So if VectorType is actually std::vector<UDT>, then swap(tmp,vectora); should be both optimal and work without modification (relying on Koenig lookup to resolve the symbol).

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

Sidebar

Related Questions

I'm trying to parallelize the following function (pseudocode): vector<int32> out; for (int32 i =
Consider the following pseudocode for the A* search algorithm : A*(G, s, h) for
I need the pseudocode for a C++ that will search through a tree to
As an example in pseudocode: if ((a mod 2) == 0) { isEven =
I'm looking for pseudocode, or sample code, to convert higher bit ascii characters (like,
Do you have pseudocode for field validation of the following items in the UK?
In VB pseudocode, I'm trying to accomplish the following using SSIS (either 2008 or
Example class in pseudocode: class SumCalculator method calculate(int1, int2) returns int What is a
Typical way of creating a CSV string (pseudocode): Create a CSV container object (like
I want something similar to the following pseudocode: myGridView.SelectedIndex = myGridView.DataKeys.IndexOf(mySpecificKey); I've done some

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.