MCPcopy Create free account
hub / github.com/davisking/dlib / thread_specific_data

Class thread_specific_data

dlib/threads/thread_specific_data_extension.h:19–132  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17 typename T
18 >
19 class thread_specific_data
20 {
21 /*!
22 CONVENTION
23 - for all valid ID:
24 (*items[ID]) == pointer to the data for thread with id ID
25 !*/
26 public:
27
28 thread_specific_data (
29 )
30 {
31 thread_end_handler_calls_left = 0;
32 }
33
34 ~thread_specific_data (
35 )
36 {
37 // We should only call the unregister_thread_end_handler function if there are
38 // some outstanding callbacks we expect to get. Otherwise lets avoid calling it
39 // since the dlib state that maintains the registered thread end handlers may have
40 // been destructed already (since the program might be in the process of terminating).
41 bool call_unregister = false;
42 m.lock();
43 if (thread_end_handler_calls_left > 0)
44 call_unregister = true;
45 m.unlock();
46
47 if (call_unregister)
48 unregister_thread_end_handler(const_cast<thread_specific_data&>(*this),&thread_specific_data::thread_end_handler);
49
50 auto_mutex M(m);
51 items.reset();
52 while (items.move_next())
53 {
54 delete items.element().value();
55 }
56 }
57
58 inline T& data (
59 ) { return get_data(); }
60
61 inline const T& data (
62 ) const { return get_data(); }
63
64 private:
65
66 T& get_data (
67 ) const
68 {
69 thread_id_type id = get_thread_id();
70 auto_mutex M(m);
71
72 T** item = items[id];
73 if (item)
74 {
75 return **item;
76 }

Callers

nothing calls this directly

Calls 5

is_dlib_threadFunction · 0.85
get_thread_idFunction · 0.70
addMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected