Add simple thread example
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
2ad9c89684
commit
276240748e
2 changed files with 33 additions and 0 deletions
|
@ -25,3 +25,6 @@ add_test(options ${CMAKE_CURRENT_BINARY_DIR}/options)
|
||||||
add_executable(scoped_map scoped_map.cpp)
|
add_executable(scoped_map scoped_map.cpp)
|
||||||
target_link_libraries(scoped_map ${EXTRA_LIBS})
|
target_link_libraries(scoped_map ${EXTRA_LIBS})
|
||||||
add_test(scoped_map ${CMAKE_CURRENT_BINARY_DIR}/scoped_map)
|
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)
|
||||||
|
|
30
src/tests/util/thread.cpp
Normal file
30
src/tests/util/thread.cpp
Normal file
|
@ -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 <thread>
|
||||||
|
#include <iostream>
|
||||||
|
#include <mutex>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
static void foo() {
|
||||||
|
static thread_local std::vector<int> 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();
|
||||||
|
}
|
Loading…
Reference in a new issue