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

The Archive Base Latest Questions

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

I wonder if size functions (size, length or whatever) are thread safe? They usually

  • 0

I wonder if size functions (size, length or whatever) are thread safe? They usually just return some private size member, as I understand. I really doubt they do any sort of calculations. They all are marked as const but are they thread-safe? for example std::list::size?

I have a lock-protected function for writing and another for reading (also lock-protected) but I wonder if my count function should also be lock-protected? IMO, it looks like a waste of response time. I don’t think it may break any iterators or fail if some member is being removed from the list at the same time (as same, as possible).

  • 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-28T15:08:51+00:00Added an answer on May 28, 2026 at 3:08 pm

    Yes, it needs to be protected by a lock. Let’s say that your implementation’s std::list::size is a 32-bit value but on your platform 32-bit reads are not atomic, they take 2 16-bit reads. In this case, a second thread may interrupt the first that was reading the size after the first read has occurred, update the size variable and then when the second 16-bit read takes place you may get a real messed up value for size.

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

Sidebar

Related Questions

As the title asks, I wonder if the size() method in the LinkedList class
Wonder if anyone can help me understand how to sum up the column of
I wonder whether someone may be able to help me please. Using some excellent
I found information from this thread and am starting to wonder if the information
I wonder if stack size can grow like heap does during runtime?
I wonder if anyone can shed some light on an issue that is driving
I wonder whether someone could help me please. Through the use of some online
In some crunching number program, I have a function which can be just 1
I'm a tad puzzled with this one. I'm trying programatically to size some UI
I wonder if this is a good solution for a medium size social site.

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.