Thrill  0.1
request_queue_impl_worker.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * foxxll/io/request_queue_impl_worker.cpp
3  *
4  * Part of FOXXLL. See http://foxxll.org
5  *
6  * Copyright (C) 2002-2005 Roman Dementiev <[email protected]>
7  * Copyright (C) 2008, 2009 Andreas Beckmann <[email protected]>
8  * Copyright (C) 2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2013 Timo Bingmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #include <cassert>
17 #include <cstddef>
18 #include <thread>
19 
22 #include <foxxll/config.hpp>
24 #include <tlx/semaphore.hpp>
25 
26 #if FOXXLL_MSVC >= 1700 && FOXXLL_MSVC <= 1800
27  #include <windows.h>
28 #endif
29 
30 namespace foxxll {
31 
33  void* (*worker)(void*), void* arg, std::thread& t,
35 {
36  assert(s() == NOT_RUNNING);
37  t = std::thread(worker, arg);
38  s.set_to(RUNNING);
39 }
40 
42  std::thread& t, shared_state<thread_state>& s, tlx::semaphore& sem)
43 {
44  assert(s() == RUNNING);
46  sem.signal();
47 #if FOXXLL_MSVC >= 1700 && FOXXLL_MSVC <= 1800
48  // In the Visual C++ Runtime 2012 and 2013, there is a deadlock bug, which
49  // occurs when threads are joined after main() exits. Apparently, Microsoft
50  // thinks this is not a big issue. It has not been fixed in VC++RT 2013.
51  // https://connect.microsoft.com/VisualStudio/feedback/details/747145
52  //
53  // All FOXXLL threads are created by singletons, which are global variables
54  // that are deleted after main() exits. The fix applied here it to use
55  // std::thread::native_handle() and access the WINAPI to terminate the
56  // thread directly (after it finished handling its i/o requests).
57 
58  WaitForSingleObject(t.native_handle(), INFINITE);
59  CloseHandle(t.native_handle());
60 #else
61  t.join();
62 #endif
63  assert(s() == TERMINATED);
65 }
66 
67 } // namespace foxxll
68 
69 /**************************************************************************/
A simple semaphore implementation using C++11 synchronization methods.
Definition: semaphore.hpp:26
void set_to(const value_type &new_state)
size_t signal()
Definition: semaphore.hpp:44
FOXXLL library namespace
void stop_thread(std::thread &t, shared_state< thread_state > &s, tlx::semaphore &sem)
void start_thread(void *(*worker)(void *), void *arg, std::thread &t, shared_state< thread_state > &s)