_configthreadlocale
Konfiguruje opcje ustawień regionalnych dla wątku.
Składnia
int _configthreadlocale( int per_thread_locale_type );
Parametry
per_thread_locale_type
Opcja do ustawienia. Jedną z opcji wymienionych w poniższej tabeli.
Wartość zwracana
Poprzedni stan ustawień regionalnych dla wątku (_DISABLE_PER_THREAD_LOCALE
lub _ENABLE_PER_THREAD_LOCALE
) lub -1 w przypadku awarii.
Uwagi
Funkcja _configthreadlocale
służy do kontrolowania użycia ustawień regionalnych specyficznych dla wątku. Użyj jednej z tych per_thread_locale_type
opcji, aby określić lub określić stan ustawień regionalnych dla każdego wątku:
Opcja | Opis |
---|---|
_ENABLE_PER_THREAD_LOCALE |
Ustaw bieżący wątek na używanie ustawień regionalnych specyficznych dla wątku. Kolejne wywołania w setlocale tym wątku mają wpływ tylko na własne ustawienia regionalne wątku. |
_DISABLE_PER_THREAD_LOCALE |
Ustaw bieżący wątek na użycie globalnych ustawień regionalnych. Kolejne wywołania w setlocale tym wątku mają wpływ na inne wątki przy użyciu globalnych ustawień regionalnych. |
0 | Pobiera bieżące ustawienie dla tego konkretnego wątku. |
Te funkcje wpływają na zachowanie setlocale
elementów , , _wsetlocale
_tsetlocale
i _setmbcp
. Gdy ustawienia regionalne poszczególnych wątków są wyłączone, każde kolejne wywołanie setlocale
lub _wsetlocale
zmiany ustawień regionalnych wszystkich wątków używających globalnych ustawień regionalnych. Gdy ustawienia regionalne poszczególnych wątków są włączone setlocale
lub _wsetlocale
mają wpływ tylko na ustawienia regionalne bieżącego wątku.
Jeśli używasz _configthreadlocale
polecenia , aby włączyć ustawienia regionalne dla każdego wątku, ustaw preferowane ustawienia regionalne w tym wątku natychmiast po wywołaniu metody setlocale
lub _wsetlocale
.
Jeśli per_thread_locale_type
nie jest jedną z wartości wymienionych w tabeli, ta funkcja wywołuje nieprawidłową procedurę obsługi parametrów zgodnie z opisem w temacie Weryfikacja parametrów. Jeśli wykonanie może kontynuować, ta funkcja ustawia errno
EINVAL
wartość i zwraca wartość -1.
Domyślnie stan globalny tej funkcji jest zakresem aplikacji. Aby zmienić to zachowanie, zobacz Stan globalny w CRT.
Wymagania
Procedura | Wymagany nagłówek |
---|---|
_configthreadlocale |
'<locale.h>' |
Przykład
// crt_configthreadlocale.cpp
//
// This program demonstrates the use of _configthreadlocale when
// using two independent threads.
//
// Compile by using: cl /EHsc /W4 crt_configthreadlocale.cpp
#include <locale.h>
#include <mbctype.h>
#include <process.h>
#include <windows.h>
#include <stdio.h>
#include <time.h>
#define BUFF_SIZE 100
// Retrieve the date and time in the current
// locale's format.
int get_time(unsigned char* str)
{
__time64_t ltime;
struct tm thetime;
// Retieve the time
_time64(<ime);
_gmtime64_s(&thetime, <ime);
// Format the current time structure into a string
// using %#x is the long date representation,
// appropriate to the current locale
if (!strftime((char *)str, BUFF_SIZE, "%#x",
(const struct tm*)&thetime))
{
printf("strftime failed!\n");
return -1;
}
return 0;
}
// This thread sets its locale to German
// and prints the time.
unsigned __stdcall SecondThreadFunc( void* /*pArguments*/ )
{
unsigned char str[BUFF_SIZE];
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
// Set the thread code page
_setmbcp(_MB_CP_ANSI);
// Set the thread locale
printf("The thread locale is now set to %s.\n",
setlocale(LC_ALL, "German"));
// Retrieve the time string from the helper function
if (get_time(str) == 0)
{
printf("The time in German locale is: '%s'\n", str);
}
_endthreadex( 0 );
return 0;
}
// The main thread spawns a second thread (above) and then
// sets the locale to English and prints the time.
int main()
{
HANDLE hThread;
unsigned threadID;
unsigned char str[BUFF_SIZE];
// Enable per-thread locale causes all subsequent locale
// setting changes in this thread to only affect this thread.
_configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
// Retrieve the time string from the helper function
printf("The thread locale is now set to %s.\n",
setlocale(LC_ALL, "English"));
// Create the second thread.
hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc,
NULL, 0, &threadID );
if (get_time(str) == 0)
{
// Retrieve the time string from the helper function
printf("The time in English locale is: '%s'\n\n", str);
}
// Wait for the created thread to finish.
WaitForSingleObject( hThread, INFINITE );
// Destroy the thread object.
CloseHandle( hThread );
}
The thread locale is now set to English_United States.1252.
The time in English locale is: 'Wednesday, May 12, 2004'
The thread locale is now set to German_Germany.1252.
The time in German locale is: 'Mittwoch, 12. Mai 2004'
Zobacz też
setlocale
, _wsetlocale
_beginthread
, _beginthreadex
ustawienia regionalne
Wielowątkowość i ustawienia regionalne