_configthreadlocale
Mengonfigurasi opsi lokal per alur.
Sintaks
int _configthreadlocale( int per_thread_locale_type );
Parameter
per_thread_locale_type
Opsi untuk diatur. Salah satu opsi yang tercantum dalam tabel berikut ini.
Nilai hasil
Status lokal per alur sebelumnya (_DISABLE_PER_THREAD_LOCALE
atau _ENABLE_PER_THREAD_LOCALE
), atau -1 pada kegagalan.
Keterangan
Fungsi _configthreadlocale
ini digunakan untuk mengontrol penggunaan lokal khusus utas. Gunakan salah satu opsi ini per_thread_locale_type
untuk menentukan atau menentukan status lokal per alur:
Opsi | Deskripsi |
---|---|
_ENABLE_PER_THREAD_LOCALE |
Buat utas saat ini menggunakan lokal khusus utas. Panggilan berikutnya ke setlocale dalam utas ini hanya memengaruhi lokal utas sendiri. |
_DISABLE_PER_THREAD_LOCALE |
Buat utas saat ini menggunakan lokal global. Panggilan berikutnya ke setlocale dalam utas ini memengaruhi utas lain menggunakan lokal global. |
0 | Mengambil pengaturan saat ini untuk utas khusus ini. |
Fungsi-fungsi ini mempengaruhi perilaku setlocale
, , _tsetlocale
_wsetlocale
, dan _setmbcp
. Ketika lokal per utas dinonaktifkan, setiap panggilan berikutnya ke setlocale
atau _wsetlocale
mengubah lokal semua utas yang menggunakan lokal global. Saat lokal per utas diaktifkan, setlocale
atau _wsetlocale
hanya memengaruhi lokal utas saat ini.
Jika Anda menggunakan _configthreadlocale
untuk mengaktifkan lokal per utas, atur lokal pilihan di utas tersebut segera setelah itu dengan panggilan ke setlocale
atau _wsetlocale
.
Jika per_thread_locale_type
bukan salah satu nilai yang tercantum dalam tabel, fungsi ini memanggil handler parameter yang tidak valid, seperti yang dijelaskan dalam Validasi parameter. Jika eksekusi diizinkan untuk melanjutkan, fungsi ini diatur errno
ke EINVAL
dan mengembalikan -1.
Secara default, status global fungsi ini dicakup ke aplikasi. Untuk mengubah perilaku ini, lihat Status global di CRT.
Persyaratan
Rutin | Header yang diperlukan |
---|---|
_configthreadlocale |
'<locale.h>' |
Contoh
// 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(<ime);
_gmtime64_s(&thetime, <ime);
// 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'
Baca juga
setlocale
, _wsetlocale
_beginthread
, _beginthreadex
Lokal
Multithreading dan lokal
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk