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

  • Home
  • SEARCH
  • 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 6780553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:31:10+00:00 2026-05-26T16:31:10+00:00

Consider the following snippet: #include <map> class A { static std::map<int,int> theMap; #pragma omp

  • 0

Consider the following snippet:

#include <map>

class A {
    static std::map<int,int> theMap;
#pragma omp threadprivate(theMap)
};

std::map<int,int> A::theMap;

Compilation with OpenMP fails with the following error message:

$ g++ -fopenmp -c main.cpp 
main.cpp:5:34: error: ‘threadprivate’ ‘A::theMap’ has incomplete type

I don’t understand this. I can compile without the #pragma directive, which should mean that std::map is not incomplete. I can also compile if theMap is a primitive type (double, int…).

How do I make a global static std::map threadprivate?

  • 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-26T16:31:11+00:00Added an answer on May 26, 2026 at 4:31 pm

    This is a compiler restriction. Intel C/C++ compiler supports C++ classes on threadprivate while gcc and MSVC currently cannot.

    For example, in MSVC (VS 2010), you will get this error (I removed the class):

    static std::map<int,int> theMap;
    #pragma omp threadprivate(theMap)
    
    error C3057: 'theMap' : dynamic initialization of 'threadprivate' symbols is not currently supported
    

    So, the workaround is pretty obvious, but dirty. You need to make a very simple thread-local storage. A simple approach would be:

    const static int MAX_THREAD = 64;
    
    struct MY_TLS_ITEM
    {
      std::map<int,int> theMap;
      char padding[64 - sizeof(theMap)];
    };
    
    __declspec(align(64)) MY_TLS_ITEM tls[MAX_THREAD];
    

    Note that the reason why I have padding is to avoid false sharing. I assume that 64-byte cache line for modern Intel x86 processors. __declspec(align(64)) is a MSVC extension that the structure is on the boundary of 64. So, any elements in tls will be located on a different cache line, resulting in no false sharing. GCC has __attribute__ ((aligned(64))).

    In order to access this simple TLS, you can do this:

    tls[omp_get_thread_num()].theMap;

    Of course, you should call this inside one of OpenMP parallel constructs. The nice thing is that OpenMP provides an abstracted thread ID in [0, N), where N is the maximum thread number. This enables a fast and simple TLS implementation. In general, a native TID from operating system is an arbitrary integer number. So, you mostly need to have a hash table whose access time is longer than a simple array.

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

Sidebar

Related Questions

Consider the following snippet: import java.util.*; public class EqualsOverload { public static void main(String[]
Consider the following snippet: public class ReflectionTest { public static void main(String[] args) {
Consider the following snippet: class Foo { public: Foo( int Value ); // other
Consider the following code snippet: std::vector<int> v; v.reserve(100); v.insert(v.end(), 100, 5); v.erase(v.begin(), v.end()); std::cout
Consider following class class test { public: test(int x){ cout<< test \n; } };
Consider the following snippet: int i = 99999999; byte b = 99; short s
Consider the following code snippet from .NET 4.0 library: private T[] array; private static
Consider the following 2 code snippets: Case 1: #include <iostream> int main() { int
Consider the following snippet: struct Foo { static const T value = 123; //Where
Consider the following snippet: struct ObjectInterface { virtual ~ObjectInterface() {} virtual void Print(std::ostream& target)

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.