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
32static std::basic_ostringstream<char> ss;
33static const char a = 'a';
34static const char b = 'b';
35static const char c = 'c';
36static const char d = 'd';
37
38void f1() {
39 std::basic_osyncstream<char> out(ss);
40 out << a;
41 std::this_thread::sleep_for(rtime: std::chrono::milliseconds(250));
42 out << b;
43}
44
45void f2() {
46 std::basic_osyncstream<char> out(ss);
47 out << c;
48 out << d;
49}
50
51int 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

source code of libcxx/test/std/input.output/syncstream/osyncstream/thread/basic.pass.cpp