Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
21.string.push_back.mt.cpp
Go to the documentation of this file.
1
30#include <string> // for string
31#include <cstddef> // for size_t
32
33#include <rw_driver.h> // for rw_test()
34#include <rw_thread.h> // for rw_thread_pool(), ...
35#include <rw_valcmp.h> // for rw_strncmp()
36
37
39#define MAX_THREADS 32
40#define MAX_LOOPS 100000
41
42const char to_append [] = {
43 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
44};
45
46const std::string shared0;
47const std::string shared1 (64, '_');
48
49
51#ifdef _RWSTD_REENTRANT
52int rw_opt_nthreads = 4;
53#else // if !defined (_RWSTD_REENTRANT)
54// in non-threaded builds use just one thread
56#endif // _RWSTD_REENTRANT
57
59
60extern "C" {
61
62static void*
63thread_func (void *arg)
64{
65 const rw_thread_t* const pthread = (rw_thread_t*)arg;
66
67 // compute an index unique to this thread
68 const std::size_t thr_inx = std::size_t (pthread->threadno) % MAX_THREADS;
69
70 RW_ASSERT (thr_inx < sizeof to_append);
71
72 // compute the expected strings unique to this thread
73 const std::string expect0 = shared0 + to_append [thr_inx];
74 const std::string expect1 = shared1 + to_append [thr_inx];
75
76 // get the data pointer and the length of each string
77 // in order to avoid referencin the string objects below
78 const char* const expect0_data = expect0.data ();
79 const std::size_t expect0_len = expect0.length ();
80
81 const char* const expect1_data = expect1.data ();
82 const std::size_t expect1_len = expect1.length ();
83
84 for (std::size_t i = 0; i != std::size_t (rw_opt_nloops); ++i) {
85
86 // create copies of global strings
87 std::string local0 (shared0);
88 std::string local1 (shared1);
89
90 // modify the local copies
91 local0.push_back (to_append [thr_inx]);
92 local1.push_back (to_append [thr_inx]);
93
94 // verify that the local copies have the expected length
95 RW_ASSERT (expect0_len == local0.length ());
96 RW_ASSERT (expect1_len == local1.length ());
97
98 // verify that the local copies have the expected data
99 RW_ASSERT (0 == rw_strncmp (expect0_data, local0.data ()));
100 RW_ASSERT (0 == rw_strncmp (expect1_data, local1.data ()));
101 }
102
103 return 0;
104}
105
106} // extern "C"
107
108
110static int
111run_test (int, char**)
112{
113 rw_info (0, 0, 0, "running %d thread%{?}s%{;}, %d iteration%{?}s%{;} each",
116
117 // create and start a pool of threads and wait for them to finish
118 const int result = rw_thread_pool (0, std::size_t (rw_opt_nthreads),
119 0, thread_func, 0);
120
121 rw_error (result == 0, 0, __LINE__,
122 "rw_thread_pool(0, %d, 0, %{#f}, 0) failed",
123 rw_opt_nthreads, thread_func);
124
125 rw_assert (0 == shared0.size (), 0, 0,
126 "shared empty string modifed: expected \"\", got %{#S}",
127 &shared0);
128
129 const std::string expect (64, '_');
130
131 rw_assert (64 == shared1.size () && std::string (64, '_') == shared1,
132 0, 0, "shared non-empty string modifed: "
133 "expected %{#S}, got %{#S}",
134 &expect, &shared1);
135
136 return result;
137}
138
139
141int main (int argc, char *argv[])
142{
143 return rw_test (argc, argv, __FILE__,
144 "lib.string.push_back",
145 "thread safety", run_test,
146 "|-nloops#0 " // must be non-negative
147 "|-nthreads#0-*", // must be in [0, MAX_THREADS]
149 int (MAX_THREADS),
151}
#define i
Definition 17.names.cpp:126
volatile int result
int rw_opt_nthreads
const std::string shared1(64, '_')
const std::string shared0
#define MAX_THREADS
const char to_append[]
#define MAX_LOOPS
int rw_opt_nloops
void run_test(const ForwardIterator &dummy, T *, Predicate *pred)
#define main
_TEST_EXPORT int rw_test(int argc, char *argv[], const char *filename, const char *clause, const char *comment, int(*testfun)(int, char **), const char *optspec,...)
_TEST_EXPORT int rw_info(int, const char *, int, const char *,...)
Definition driver.cpp:1681
_TEST_EXPORT int rw_assert(int, const char *, int, const char *,...)
Definition driver.cpp:1630
_TEST_EXPORT int rw_error(int, const char *, int, const char *,...)
Definition driver.cpp:1613
#define RW_ASSERT(expr)
_TEST_EXPORT int rw_thread_pool(rw_thread_t *, _RWSTD_SIZE_T, rw_thread_attr_t *, rw_thread_proc *, void **, _RWSTD_SIZE_T=0)
_TEST_EXPORT int rw_strncmp(const char *, const char *, _RWSTD_SIZE_T=_RWSTD_SIZE_MAX, int=CMP_NULTERM)
long threadno
Definition rw_thread.h:43
int arg
Definition locale.cpp:2806