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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:25:01+00:00 2026-06-12T08:25:01+00:00

I am looking for some advice about OpenMP. I am confused about the use

  • 0

I am looking for some advice about OpenMP. I am confused about the use of threadprivate variables. The problem can be presented in the following example:

-----------

// The code below is located the dynamically loaded DLL.
/* Global variable. */
int *p;
#pragma omp threadprivate(p)

extern "C" __declspec(dllexport) int MyFunc1(void)
{
  int i;
  #pragma omp parallel for
  for (i = 0; i < n; i++) {
     MyFunc2(i);
  }
   return TRUE;
}

void MyFunc2(void)
{
    p  = malloc(sizeof(int));
   *p  = 0;
    printf(“value = %d”,*p);
    free(p);
}

-----------

Here I want each thread to have a separate copy of a global thread-independent variable that will be visible in all functions of the thread. The variable will be initialized and destroyed within the thread.

The “problem” here is that all code including definition of the global variable “p” is located the dynamically loaded DLL (via LoadLibrary).

Microsoft sayshttp://msdn.microsoft.com/en-us/library/2z1788dd.aspx: “You cannot use threadprivate in any DLL that will be loaded via LoadLibrary. This includes DLLs that are loaded with /DELAYLOAD (Delay Load Import), which also uses LoadLibrary.” So, if I got it right, the above code is not correct – threadprivate variables and dynamically loaded DLL do not mix.

To verify this I created a test project that dynamically loads a DLL add runs a function in parallel using threadprivate as described above. It all worked just fine!

Hmmm … Now I am confused because that project was not supposed to work.

Can I really use thread private variables in dinamic DLLs or is there a trick to this?

Thank you,

Alex

  • 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-12T08:25:02+00:00Added an answer on June 12, 2026 at 8:25 am

    threadprivate in the MS OpenMP implementation is translated to __declspec(thread) which puts the declared variable in the static TLS (Thread-Local Storage). When a program is started the size of the TLS is determined by taking into account the TLS size required by the executable as well as the TLS requirements of all other implicitly loaded DLLs. When you load another DLL dynamically with LoadLibrary or unload it with FreeLibrary, the system has to examine all running threads and to enlarge or compact their TLS storage accordingly. According to KB118816:

    This process is too much for operating systems to manage, which can cause an exception either when the DLL is dynamically loaded or code references the data.

    Access to such variables is considered undefined behaviour. It works in your case but it doesn’t mean that it will work everywhere and every time. Here you can read why exactly it would most likely fail on Windows XP/2003 and earlier Windows versions. According to the same source, implicit TLS handling was rewritten in Windows Vista and so OpenMP threadprivate and __declspec(thread) should function correctly in run-time loaded DLLs since then. The proposed solution is to use TlsAlloc instead.

    DWORD dwTlsIdx;
    
    extern "C" __declspec(dllexport) int MyFunc1(void)
    {
       int i;
       #pragma omp parallel for
       for (i = 0; i < n; i++) {
          MyFunc2(i);
       }
       return TRUE;
    }
    
    void MyFunc2(void)
    {
        int **pp = (int **)TlsGetValue(dwTlsIdx);
        *pp = malloc(sizeof(int));
        **pp  = 0;
        printf(“value = %d”,**pp);
        free(*pp);
    }
    

    dwTlsIdx should be initialised on process attach in DllMain with a call to TlsAlloc. Enough memory to hold an int * should be allocated on thread attach and its address should be set as the value of the dwTlsIdx TLS index. Or you could do it in on the first call to MyFunc2 instead:

    void MyFunc2(void)
    {
        int **pp = (int **)TlsGetValue(dwTlsIdx);
        if (pp == NULL)
        {
           pp = malloc(sizeof(int *));
           TlsSetValue(dwTlsIdx, pp);
        }
        *pp = malloc(sizeof(int));
        **pp  = 0;
        printf(“value = %d”,**pp);
        free(*pp);
    }
    

    See here for more details (with error checking).

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

Sidebar

Related Questions

I am looking for some advice about what game engine I should use. I
I'm looking for some advice on how to go about reading the online documentation
I'm looking for some advice on whether or not I should use a separate
Problem Hey folks. I'm looking for some advice on python performance. Some background on
I'm looking for some general advice on the most efficient way to go about
I am looking over some code that has heavy use of Java's instanceof operator.
Looking for some general advice and tips about using cancan on our latest rails3
I'm looking for some advice on how to optimise the following process: App reads
I am looking for some advice about structuring Delphi programs for maintainability. I've come
Im looking for some advice on how I should go about a solution. I

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.