_configthreadlocale

配置每个线程的区域设置选项。

语法

int _configthreadlocale( int per_thread_locale_type );

参数

per_thread_locale_type
要设置的选项。 其中一个选项已在下表中列出。

返回值

以前的每个线程的区域设置状态(_DISABLE_PER_THREAD_LOCALE_ENABLE_PER_THREAD_LOCALE),在失败时为 -1。

备注

_configthreadlocale 函数用于控制特定于线程的区域设置的使用。 请使用以下 per_thread_locale_type 选项之一指定或确定每个线程的区域设置状态:

选项 说明
_ENABLE_PER_THREAD_LOCALE 使当前线程使用特定于线程的区域设置。 此线程中对 setlocale 的后续调用只会影响线程自己的区域设置。
_DISABLE_PER_THREAD_LOCALE 使当前线程使用全局区域设置。 此线程中对 setlocale 的后续调用会影响其他使用全局区域设置的线程。
0 检索此特定线程的当前设置。

这些函数将影响 setlocale_tsetlocale_wsetlocale_setmbcp 的行为。 如果禁用了每个线程的区域设置,则对 setlocale_wsetlocale 的任何后续调用都将更改所有使用全局区域设置的线程的区域设置。 当每个线程的区域设置都已启用时,setlocale_wsetlocale 只会影响当前线程的区域设置。

如果使用 _configthreadlocale 来启用每个线程的区域设置,请通过在此后立即调用 setlocale_wsetlocale 来设置该线程的首选区域设置。

如果 per_thread_locale_type 不是表中列出的值之一,此函数将调用无效的参数处理程序,如参数验证中所述。 如果允许执行继续,则该函数将 errno 设置为 EINVAL 并返回 -1。

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

例程 必需的标头
_configthreadlocale `<locale.h>`

示例

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

另请参阅

setlocale_wsetlocale
_beginthread_beginthreadex
区域设置
多线程和区域设置