You are currently offline, waiting for your internet to reconnect

BUG: _wsetlocale Is Not Threadsafe

This article was previously published under Q304497
This article has been archived. It is offered "as is" and will no longer be updated.
A heap corruption can occur if _wsetlocale is called simultaneously from multiple threads.
_wsetlocale does not synchronize access to a local static variable, outwlocale.
To work around this, you must synchronize calls to _wsetlocale.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
The C Runtime locale mechanism in Microsoft Visual C++ is distinct from the locale mechanism in Microsoft Windows. Though Windows supports per-thread locales, C Runtime supports only a single global locale for any program.

If multiple threads call _wsetlocale, then no thread's locale can be trusted. For example, if one thread calls _wsetlocale for French and tries to use that locale for _wcslwr, and another thread calls _wsetlocale for English before the first thread executes the _wcslwr function call, then the first thread uses the English locale instead of the French locale.

Steps to Reproduce the Behavior

#define _UNICODE#include <stdio.h>#include <locale.h>#include <process.h>void __cdecl test(void *i) {	_wsetlocale(LC_ALL, L"English");	_wsetlocale(LC_ALL, L"French");	_wsetlocale(LC_ALL, L"German");	_wsetlocale(LC_ALL, L"English");	printf("%d exit\n",i);}void wmain () {	for (int i=0; i<30; ++i) _beginthread(test,0,(void*)i);	getchar();}				

Article ID: 304497 - Last Review: 01/06/2015 06:54:34 - Revision: 1.0

Microsoft Visual C++ 6.0 Service Pack 5

  • kbnosurvey kbarchive kbbug kbfix KB304497