Share via


_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 擷取這個特定執行緒的目前設定。

這些函式會影響 、 _tsetlocale_wsetlocale_setmbcp 的行為 setlocale 。 停用每個執行緒地區設定時,任何後續呼叫 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
地區設定
多執行緒和地區設定