_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.

表に示されている値の 1 つでない場合per_thread_locale_typeは、「パラメーターの検証」で説明されているように、この関数は無効なパラメーター ハンドラーを呼び出します。 実行の継続が許可された場合、この関数は errnoEINVAL に設定し、-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
ロケール
マルチスレッドとロケール