xref: /llvm-project/libcxx/test/std/input.output/syncstream/osyncstream/thread/basic.pass.cpp (revision 7cc72a0a2ec22855572d96411febd4f2c4ac5a49)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: no-threads
12 // UNSUPPORTED: libcpp-has-no-experimental-syncstream
13 
14 // <syncstream>
15 
16 // template <class charT, class traits, class Allocator>
17 // class basic_osyncstream;
18 
19 // Basic test whether the code works in a threaded environment.
20 // Using timing the output order should be stable.
21 // several_threads.pass.cpp tests with more threads.
22 
23 #include <syncstream>
24 #include <sstream>
25 #include <mutex>
26 #include <thread>
27 #include <cassert>
28 #include <iostream>
29 
30 #include "test_macros.h"
31 
32 static std::basic_ostringstream<char> ss;
33 static const char a = 'a';
34 static const char b = 'b';
35 static const char c = 'c';
36 static const char d = 'd';
37 
f1()38 void f1() {
39   std::basic_osyncstream<char> out(ss);
40   out << a;
41   std::this_thread::sleep_for(std::chrono::milliseconds(250));
42   out << b;
43 }
44 
f2()45 void f2() {
46   std::basic_osyncstream<char> out(ss);
47   out << c;
48   out << d;
49 }
50 
main(int,char **)51 int main(int, char**) {
52   std::thread t1(f1);
53   std::thread t2(f2);
54   t1.join();
55   t2.join();
56 
57   assert(ss.str() == "cdab");
58 
59   return 0;
60 }
61