BUG: Registry access from multiple threads might fail

Article translations Article translations
Article ID: 176906 - View products that this article applies to.
This article was previously published under Q176906
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page


If you simultaneously access the same registry key from multiple threads in a single process, an error might occur. For example, if several threads in a carefully designed multi-threaded Win32 application try to open the same registry key using RegOpenKeyEx()in a loop, the function could fail with the following error code of 6:
This error does not occur across process boundaries. Thus, two single- threaded processes that are competing for the same registry key will not be affected by this bug.


This behavior is intermittent and is the result of a race condition between the threads simultaneously accessing the same registry key.


There are several possible workarounds for this situation:
  1. Do not access the same registry location from multiple threads in a single process.
  2. Synchronize access to the registry location. You can do this by serializing access to the code that accesses the registry with a critical section or other Win32 synchronization mechanisms.
  3. When this error occurs, Sleep() a short time and try to access the registry key again. It is a race condition that causes this problem, so this workaround is not guaranteed to work. It is possible that the registry key is indeed invalid. However, this is the easiest solution to work into an existing application.


Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.


The following sample code illustrates the problem with the RegOpenKeyEx() function. Note that the error only happens intermittently with the following code.

Sample code

   #include <windows.h>
   #include <winbase.h>
   #include <stdio.h>

   #define ITERATIONS 1000
   VOID ThreadFunc(LPVOID);

   void main(void)
    int numThreads=100;
    DWORD threadID;
    int i;

     for (i=0; i<numThreads; i++)
       CreateThread(0, 0,(LPTHREAD_START_ROUTINE) ThreadFunc,
                    0, 0, &threadID);

   VOID ThreadFunc(LPVOID)
     LONG rc;
     HKEY hKey;

     for(int i=0; i < ITERATIONS; i++)
       rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft",
                          0, KEY_ALL_ACCESS, &hKey);

       if (rc != ERROR_SUCCESS)
         printf("Iteration(%d): Thread Id: %d, Error=%d\r\n", i,
                                   GetCurrentThreadId(), rc);
          rc = RegCloseKey(hKey);


Article ID: 176906 - Last Review: February 24, 2014 - Revision: 4.1
  • Microsoft Win32 Application Programming Interface, when used with:
    • Microsoft Windows NT 4.0
    • Microsoft Windows 2000 Standard Edition
kbnosurvey kbarchive kbapi kbbug kberrmsg kbkernbase kbpending kbregistry KB176906

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com