_configthreadlocale

Konfiguriert Optionen für threadspezifische Gebietsschemas.

Syntax

int _configthreadlocale( int per_thread_locale_type );

Parameter

per_thread_locale_type
Die festzulegende Option. Eine der in der folgenden Tabelle aufgelisteten Optionen.

Rückgabewert

Der vorherige Status des threadspezifischen Gebietsschemas (_DISABLE_PER_THREAD_LOCALE oder _ENABLE_PER_THREAD_LOCALE) oder "– 1", wenn ein Fehler auftritt.

Hinweise

Mit der _configthreadlocale-Funktion wird die Verwendung von threadspezifischen Gebietsschemas gesteuert. Verwenden Sie eine der folgenden per_thread_locale_type Optionen, um den Gebietsschemastatus pro Thread anzugeben oder zu bestimmen:

Option Beschreibung
_ENABLE_PER_THREAD_LOCALE Legen Sie fest, dass der aktuelle Thread ein threadspezifisches Gebietsschema verwendet. Nachfolgende Aufrufe von setlocale in diesem Thread betreffen nur das eigene Gebietsschema des Threads.
_DISABLE_PER_THREAD_LOCALE Legen Sie fest, dass der aktuelle Thread das globale Gebietsschema verwendet. Nachfolgende Aufrufe von setlocale in diesem Thread wirken sich auch auf andere Threads aus, die das globale Gebietsschema verwenden.
0 Ruft die aktuelle Einstellung für diesen speziellen Thread ab.

Diese Funktionen wirken sich auf das Verhalten von setlocale, _tsetlocale, , _wsetlocaleund _setmbcp. Wenn das Gebietsschema pro Thread deaktiviert ist, werden alle nachfolgenden Aufrufe setlocale oder _wsetlocale Änderungen des Gebietsschemas aller Threads, die das globale Gebietsschema verwenden, geändert. Wenn das threadspezifische Gebietsschema aktiviert ist, wirken sich setlocale oder _wsetlocale nur auf das Gebietsschema des aktuellen Threads aus.

Wenn Sie ein Gebietsschema pro Thread aktivieren, legen Sie _configthreadlocale das bevorzugte Gebietsschema in diesem Thread unmittelbar nach einem Aufruf von setlocale oder _wsetlocale.

Wenn per_thread_locale_type keine der werte in der Tabelle aufgeführt ist, ruft diese Funktion den ungültigen Parameterhandler auf, wie in der Parameterüberprüfung beschrieben. Wenn die weitere Ausführung zugelassen wird, setzt diese Funktion errno auf EINVAL und gibt "-1" zurück.

Standardmäßig gilt der globale Zustand dieser Funktion für die Anwendung. Informationen zum Ändern dieses Verhaltens finden Sie im Global state in the CRT.

Anforderungen

Routine Erforderlicher Header
_configthreadlocale "<locale.h>"

Beispiel

// 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(&ltime);
    _gmtime64_s(&thetime, &ltime);

    // 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'

Siehe auch

setlocale, _wsetlocale
_beginthread, _beginthreadex
Gebietsschema
Multithreading und Gebietsschemas