Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
THREAD_SAFE_LOCALE.cpp
Go to the documentation of this file.
1// checking if locale is thread safe
2
3
25// determines whether each thread has its own C locale environment
26// that's independent of those of all other threads or whether the
27// C locale facility is process global (i.e., shared among all
28// threads)
29
30
31#include <locale.h> // for LC_ALL, setlocale()
32#include <string.h> // for strcpy()
33
34#include "locale_names.h" // for test_locale_names
35#include "thread.h" // for pthread_t, pthread_create
36
37
38static char
39thread_locale [3][1024];
40
41
42extern "C" {
43
44void* thread_routine (void *arg)
45{
46 const char* const name = setlocale (LC_ALL, 0);
47
48 if (name) {
49 const unsigned long inx = (unsigned long)arg;
50
51 strcpy (thread_locale [inx], name);
52 }
53
54 return 0;
55}
56
57} // extern "C"
58
59int main ()
60{
61 const char* locale_name = setlocale (LC_ALL, "C");
62
63 // save the main thread's locale at index 0
64 strcpy (thread_locale [0], locale_name);
65
66 pthread_t thread [2];
67
68 // create one thread and have it save its locale name at index 1
69 if (pthread_create (thread + 0, 0, thread_routine, (void*)1))
70 return 4;
71
72 // find a named locale different from the initial one
73 for (unsigned i = 0; i != nlocales; ++i) {
74 // set the main thread's locale to one that's different
75 // from that of the first thread
76 locale_name = setlocale (LC_ALL, test_locale_names [i]);
77 if (locale_name && strcmp (locale_name, thread_locale [0]))
78 break;
79 }
80
81 // create another thread and have it save its locale name at index 2
82 if (pthread_create (thread + 1, 0, thread_routine, (void*)2)) {
83 return 3;
84 }
85
86 // join both (sub)threads
87 if (pthread_join (thread [0], 0) || pthread_join (thread [1], 0))
88 return 2;
89
90 // return 0 (success) when the (sub)thread's locale is different
91 // from the main thread's locale and 1 (failure) when they are
92 // the same
93 return 0 == strcmp (thread_locale [0], thread_locale [1])
94 || 0 == strcmp (thread_locale [0], thread_locale [2]);
95}
#define i
Definition 17.names.cpp:126
#define inx
Definition 17.names.cpp:484
const char *const test_locale_names[]
const unsigned nlocales
void * thread_routine(void *arg)
int main()
#define LC_ALL
const char * name
Definition codecvt1.cpp:72
int arg
Definition locale.cpp:2806