#define _GNU_SOURCE
#include <pthread.h>
#include <locale.h>
#include <time.h>
#include <stdlib.h>
struct tm tm = { 0, };
void *
thread_func()
{
char buf[256];
int n;
locale_t l;
l = newlocale(LC_ALL_MASK, "ko_KR.UTF-8", 0);
uselocale(l);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
uselocale(LC_GLOBAL_LOCALE);
freelocale(l);
}
int
main(int argc, char *argv[])
{
pthread_t t;
char buf[256];
int n;
setlocale(LC_ALL, "C");
pthread_create(&t, 0, thread_func, 0);
pthread_detach(t);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
sleep(1);
n = strftime(buf, 256, "%b %A %a", &tm);
buf[n] = '\0';
puts(buf);
}
실행해 보면 이렇게 두 개 스레드가 다른 로케일을 사용한다.
$ ./a.out
Jan Sunday Sun
1월 일요일 일
Jan Sunday Sun
1월 일요일 일
Jan Sunday Sun
1월 일요일 일
Jan Sunday Sun