Share via


_configthreadlocale

針對每一個執行緒地區設定選項。

int _configthreadlocale(
   int type
);

參數

  • type
    要設定的選項。 其中一個選項下表中列出。

傳回值

上述每個執行緒地區設定狀態 (_DISABLE_PER_THREAD_LOCALE 或 _ENABLE_PER_THREAD_LOCALE),或者在 -1 失敗。

備註

_configurethreadlocale 函式是用來控制項所使用的地區設定。 使用其中一個選項指定或判斷每個執行緒地區設定狀態:

  • _ENABLE_PER_THREAD_LOCALE
    將目前的執行緒使用的地區設定。 對 setlocale 的後續呼叫在此執行緒只會影響執行緒的地區設定。

  • _DISABLE_PER_THREAD_LOCALE
    將目前的執行緒使用全域地區設定。 使用全域地區設定,對 setlocale 的後續呼叫在此執行緒會影響其他執行緒。

  • 0
    擷取這個特定執行緒的目前設定。

這些函式會影響 setlocale、 _tsetlocale、 _wsetlocale、 _beginthread和_beginthreadex的行為。 如果另一個方法來建立執行緒,地區設定不會影響其他執行緒的作用。

在個別執行緒地區設定停用時, setlocale 或 _wsetlocale 的任何後續的變更執行緒地區設定。 在個別執行緒地區設定時, setlocale 或 _wsetlocale 只會影響目前執行緒的地區設定。

如果您使用 _configurethreadlocale 啟用個別執行緒地區設定,我們建議您呼叫 setlocale 或 _wsetlocale 狀態之後設定該執行緒的慣用地區設定。

如果 type 不在表格中列出的其中一個值,這個函式叫用無效的參數處理常式,如 參數驗證中所述。 如果執行允許繼續,這個函式會將 errno 設定為 EINVAL 並傳回 -1。

需求

程序

必要的標頭檔

_configthreadlocale

<locale.h>

範例

// crt_configthreadlocale.cpp
// 
// This program demonstrates the use of _configthreadlocale when
// using is two independent threads.
//

#include <locale.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];

    // 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];

    // Configure per-thread locale to cause all subsequently created 
    // threads to have their own locale.
    _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 );
}
         

.NET Framework 對等用法

不適用。不過,請參閱 使用 CurrentCulture 屬性

請參閱

參考

setlocale _wsetlocale

_beginthread _beginthreadex

地區設定

多執行緒和地區設定