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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:39:48+00:00 2026-05-20T08:39:48+00:00

I thought thread safe, in particular, means it must satisfy the need for multiple

  • 0

I thought thread safe, in particular, means it must satisfy the need for multiple threads to access the same shared data. But, it seems this definition is not enough.

Can anyone please list out the things to be done or taken care of to make an application thread safe. If possible, give an answer with respect to C/C++ language.

  • 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-20T08:39:49+00:00Added an answer on May 20, 2026 at 8:39 am

    There are several ways in which a function can be thread safe.

    It can be reentrant. This means that a function has no state, and does not touch any global or static variables, so it can be called from multiple threads simultaneously. The term comes from allowing one thread to enter the function while another thread is already inside it.

    It can have a critical section. This term gets thrown around a lot, but frankly I prefer critical data. A critical section occurs any time your code touches data that is shared across multiple threads. So I prefer to put the focus on that critical data.

    If you use a mutex properly, you can synchronize access to the critical data, properly protecting from thread unsafe modifications. Mutexes and Locks are very useful, but with great power comes great responsibility. You must not lock the same mutex twice within the same thread (that is a self-deadlock). You must be careful if you acquire more than one mutex, as it increases your risk for deadlock. You must consistently protect your data with mutexes.

    If all of your functions are thread safe, and all of your shared data properly protected, your application should be thread safe.

    As Crazy Eddie said, this is a huge subject. I recommend reading up on boost threads, and using them accordingly.

    low-level caveat: compilers can reorder statements, which can break thread safety. With multiple cores, each core has its own cache, and you need to properly sync the caches to have thread safety. Also, even if the compiler doesn’t reorder statements, the hardware might. So, full, guaranteed thread safety isn’t actually possible today. You can get 99.99% of the way there though, and work is being done with compiler vendors and cpu makers to fix this lingering caveat.

    Anyway, if you’re looking for a checklist to make a class thread-safe:

    • Identify any data that is shared across threads (if you miss it, you can’t protect it)
    • create a member boost::mutex m_mutex and use it whenever you try to access that shared member data (ideally the shared data is private to the class, so you can be more certain that you’re protecting it properly).
    • clean up globals. Globals are bad anyways, and good luck trying to do anything thread-safe with globals.
    • Beware the static keyword. It’s actually not thread safe. So if you’re trying to do a singleton, it won’t work right.
    • Beware the Double-Checked Lock Paradigm. Most people who use it get it wrong in some subtle ways, and it’s prone to breakage by the low-level caveat.

    That’s an incomplete checklist. I’ll add more if I think of it, but hopefully it’s enough to get you started.

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

Sidebar

Related Questions

This isn't a question but I thought I'd start a thread where links to
The Application static members are supposed to be thread safe: The public static (Shared
I'm wondering whether prepared statements in Android (instances of SQLiteStatement) are thread-safe. In particular
What would be the least-slow thread-safe mechanism for controlling multiple accesses to a collection
so far I thought that any operation done on shared object (common for multiple
I thought I heard that py2exe was able to do this, but I never
I thought people would be working on little code projects together, but I don't
I thought I understood Java generics pretty well, but then I came across the
PHP, 5.3 non thread safe window, also proven on PHP 5.2.9 on linux. Sample
In C#, a class level variable would not be thread-safe (any exceptions to this?

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.