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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:55:14+00:00 2026-06-10T03:55:14+00:00

Possible Duplicate: C++11 thread_local in gcc – alternatives Is there any way to fully

  • 0

Possible Duplicate:
C++11 thread_local in gcc – alternatives
Is there any way to fully emulate thread_local using GCC's __thread?

I wanted to use the c++11 thread_local to create and use thread_local variable but as it is not yet supported by gcc, I am using gcc specific __thread. The way I declared the variable is

myClass
{
public:

  static __thread int64_t m_minInt;

};
__thread int64_t myClass::m_minInt = 100;

When I compile it, I get an error like

error: ‘myClass::minInt’ is thread-local and so cannot be dynamically initialized

How to properly do it?

PS: gcc version: 4.6.3

  • 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-10T03:55:16+00:00Added an answer on June 10, 2026 at 3:55 am

    You need to use lazy initialization.

    myClass
    {
    public:
    
      static __thread int64_t m_minInt;
      static __thread bool m_minIntInitialized;
    
      static int64_t getMinInt();
    };
    __thread int64_t myClass::m_minInt;
    __thread bool myClass::m_minIntInitialized;
    
    
    int64_t myClass::getMinInt()
    {
      if (!m_minIntInitialized)  // note - this is (due to __thread) threadsafe
      {
        m_minIntInitialized = true;
        m_minInt = 100;
      }
    
      return m_minInt;
    }
    

    m_minIntInitialized is guaranteed to be zero.

    In most cases (ELF specification) it is placed to .tbss section, which is zero-initialized.

    For C++ – http://en.cppreference.com/w/cpp/language/initialization

    For all other non-local static and thread-local variables, Zero
    initialization takes place. In practice, variables that are going to
    be zero-initialized are placed in the .bss segment of the program
    image, which occupies no space on disk, and is zeroed out by the OS
    when loading the program.

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

Sidebar

Related Questions

Possible Duplicate: Is there any way to kill a Thread in Python? So this
Possible duplicate question: Is there a way to indefinitely pause a thread? In my
Possible Duplicate: Why should I use a thread vs using a process? I have
Possible Duplicate: .NET Is there a way to get the parent thread id? Is
Possible Duplicate: Threads vs Processes in Linux To implement multi-tasks programs we could use
Possible Duplicate: How do I make a request using HTTP basic authentication with PHP
Possible Duplicate: Compare using Thread.Sleep and Timer for delayed execution I am considering whether
Possible Duplicate: c++ multithread I use c++ to implement a thread class. The code
Possible Duplicate: When and how should I use a ThreadLocal variable? What I know
Possible Duplicate: I need a good way to get data from a thread to

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.