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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:51:01+00:00 2026-05-10T20:51:01+00:00

This question is about using getter methods of a singleton object in worker threads.

  • 0

This question is about using getter methods of a singleton object in worker threads. Here is some pseudo code first:

// Singleton class which contains data class MyData {     static MyData* sMyData ;      int  mData1[1024];     int  mData2[1024];     int  mData3[1024];      MyData* getInstance()     {         // sMyData is created in the very beginning.         return sMyData ;     }      void getValues(int idx, int& data1,int& data2,int& data3)     {         data1 = mData1[idx];         data2 = mData2[idx];         data3 = mData3[idx];     }      int* getData1()     {         return &mData1[0];     } }  class MyThread {     void workerMethod()     {         MyData* md = MyData::getInstance();          int d1,d2,d3;         md->getValue( 12, d1,d2,d3 );          int* data1 = md->getData1();         d1 = data1[34];     } } 

Now as you see I have some getter methods (all read-only), MyData::getInstance(), MyData::getValue() and MyData::getData1(). The 1st question is how thread-safe these methods are ?

Since they are often-called methods, protecting those methods with mutex is something I am trying to avoid.

The 2nd question is: what is the suggested way of reading data from central sources in a multi-thread application, especially in worker methods.

Thanks !

Paul

  • 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. 2026-05-10T20:51:02+00:00Added an answer on May 10, 2026 at 8:51 pm

    Provided that no other thread will try to write to the data in your singleton object, you don’t need to protect them: by definition, multiple readers in the absence of a writer is thread-safe. This is a common pattern where the program’s initialization code sets up a singleton, which is then only read from by worker threads.

    However, if any thread ever writes to this data while others are reading from it, you must protect it in some way. If you have lots of readers and only the occasional writer, it is worth considering some sort of ‘read-write’ lock, which allows multiple readers in the absence of any writers.

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

Sidebar

Ask A Question

Stats

  • Questions 98k
  • Answers 98k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Apparently this was a common enough problem to be on… May 11, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer WPF is optimized to only re-draw items when absolutely necessary,… May 11, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer I'd do for i in `seq 0 2 10`; do… May 11, 2026 at 7:33 pm

Related Questions

This question is about the behavior of an object with methods added to its
I'm using ActiveRecord to maintain information about users. The User class has the expected
I'm using TPTP to profile some slow running Java code an I came across
I have a Struts + Velocity structure like for example, a Person class, whose

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.