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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:16:30+00:00 2026-06-06T08:16:30+00:00

which is better or faster, A or B? std::deque<Myclass> queue; … // do something

  • 0

which is better or faster, A or B?

std::deque<Myclass> queue;
... // do something
std::size_t size = 0;
... // create n threads, one push queue and others pop queue.

// a thread do below
#ifdef A
pthread_rwlock_wrlock(&rwlock);
queue.push_front(myobj);
size = queue.size();
pthread_rwlock_unlock(&rwlock);
#endif

#ifdef B
pthread_rwlock_wrlock(&rwlock);
queue.push_front(myobj); 
pthread_rwlock_unlock(&rwlock);
// if there is some operation,
// I think this B is better,
// because I should get the newest size.
pthread_rwlock_rdlock(&rwlock);
size = queue.size();
pthread_rwlock_unlock(&rwlock);
#endif

// other threads do below
pthread_rwlock_wrlock(&rwlock);
queue.pop_back(); 
pthread_rwlock_unlock(&rwlock);

That’s my not understanding.

Any information or suggestion is great help for me!

Apologize for my poor English!

  • 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-06T08:16:31+00:00Added an answer on June 6, 2026 at 8:16 am

    Your A version is faster as you need to acquire the lock only once (you need to acquire the write lock once at any rate). Also, the returned size will be the latest (at the time of push) as no other thread can access the queue at the same time.

    If you do B, you might get a different answer for size (as you noted), however, once you release the lock (in either scenarios) the size variable can always hold stale value (a thread might have updated the queue by the time you want to use the value in size)

    Also, the readers should query the queue for its size (within the protected section) to avoid an exception if the queue is empty

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

Sidebar

Related Questions

Which one is better/faster/preferred 1: mov eax, 5 push eax mov eax, [someAddress] push
Between HtmlSelect and DropDownList, which one has a better performance (initialized and rendered faster)?
Which one is better and faster? why? using only one file for styling: css/style.css
Which one of the 2 options is better and faster to clear an ArrayList,
Which of these two statements is faster/better practice? myList.Where(x => { bool itemOne= x.ItemOne
Try to see which cast is faster (not necessary better): new c++ case or
Which option is better and faster? Insertion of data after creating index on empty
I'm looking for some data to help me decide which would be the better/faster
I was wondering which is a better/faster/more efficient way of turning arbitrary strings into
Which of these two is better/faster? var a = $('.listItem', $('#myList')); vs var a

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.