From 276240748edfb4e85f5d386df3ee054bb4ac7b8e Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Mon, 12 Aug 2013 14:10:21 -0700 Subject: [PATCH] Add simple thread example Signed-off-by: Leonardo de Moura --- src/tests/util/CMakeLists.txt | 3 +++ src/tests/util/thread.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/tests/util/thread.cpp diff --git a/src/tests/util/CMakeLists.txt b/src/tests/util/CMakeLists.txt index 8b9627e29..b1322d6d9 100644 --- a/src/tests/util/CMakeLists.txt +++ b/src/tests/util/CMakeLists.txt @@ -25,3 +25,6 @@ add_test(options ${CMAKE_CURRENT_BINARY_DIR}/options) add_executable(scoped_map scoped_map.cpp) target_link_libraries(scoped_map ${EXTRA_LIBS}) add_test(scoped_map ${CMAKE_CURRENT_BINARY_DIR}/scoped_map) +add_executable(thread thread.cpp) +target_link_libraries(thread ${EXTRA_LIBS}) +add_test(thread ${CMAKE_CURRENT_BINARY_DIR}/thread) diff --git a/src/tests/util/thread.cpp b/src/tests/util/thread.cpp new file mode 100644 index 000000000..7373a7570 --- /dev/null +++ b/src/tests/util/thread.cpp @@ -0,0 +1,30 @@ +/* +Copyright (c) 2013 Microsoft Corporation. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. + +Author: Leonardo de Moura +*/ +#include +#include +#include +#include + +static void foo() { + static thread_local std::vector v(1024); + if (v.size() != 1024) { + std::cerr << "Error\n"; + exit(1); + } +} + +static void tst1() { + unsigned n = 5; + for (unsigned i = 0; i < n; i++) { + std::thread t([](){ foo(); }); + t.join(); + } +} + +int main() { + tst1(); +}