Apache Log4cxx  Version 1.2.0
Loading...
Searching...
No Matches
threadutility.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _LOG4CXX_THREADUTILITY_H
19#define _LOG4CXX_THREADUTILITY_H
20
21#include <thread>
22#include <functional>
23#include <memory>
24
25#include "log4cxx/logstring.h"
26#include "widelife.h"
27
28namespace LOG4CXX_NS
29{
30namespace helpers
31{
32
38typedef std::function<void()> ThreadStartPre;
39
49typedef std::function<void( LogString threadName,
50 std::thread::id threadId,
51 std::thread::native_handle_type nativeHandle )> ThreadStarted;
52
57typedef std::function<void()> ThreadStartPost;
58
60{
65};
66
67class ThreadUtility;
69
70class LOG4CXX_EXPORT ThreadUtility
71{
72 private:
73 friend class LOG4CXX_NS::helpers::WideLife<ThreadUtility>;
75
76 LOG4CXX_NS::helpers::ThreadStartPre preStartFunction();
77 LOG4CXX_NS::helpers::ThreadStarted threadStartedFunction();
78 LOG4CXX_NS::helpers::ThreadStartPost postStartFunction();
79
80 LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(priv_data, m_priv)
81 public:
83
85
91
98 ThreadStarted started,
99 ThreadStartPost post_start );
100
107
113 std::thread::id thread_id,
114 std::thread::native_handle_type native_handle);
115
122
126 template<class Function, class... Args>
127 std::thread createThread(LogString name,
128 Function&& f,
129 Args&& ... args)
130 {
131 LOG4CXX_NS::helpers::ThreadStartPre pre_start = preStartFunction();
132 LOG4CXX_NS::helpers::ThreadStarted thread_start = threadStartedFunction();
133 LOG4CXX_NS::helpers::ThreadStartPost post_start = postStartFunction();
134
135 if ( pre_start )
136 {
137 pre_start();
138 }
139
140 std::thread t( f, args... );
141
142 if ( thread_start )
143 {
144 thread_start( name,
145 t.get_id(),
146 t.native_handle() );
147 }
148
149 if ( post_start )
150 {
151 post_start();
152 }
153
154 return t;
155 }
156};
157
158} /* namespace helpers */
159} /* namespace log4cxx */
160
161#endif
Definition: threadutility.h:71
void configureFuncs(ThreadStartPre pre_start, ThreadStarted started, ThreadStartPost post_start)
Configure the thread functions that log4cxx will use.
void preThreadBlockSignals()
A pre-start thread function that blocks signals to the new thread (if the system has pthreads).
void threadStartedNameThread(LogString threadName, std::thread::id thread_id, std::thread::native_handle_type native_handle)
A thread_started function that names the thread using the appropriate system call.
static ThreadUtility * instance()
std::thread createThread(LogString name, Function &&f, Args &&... args)
Start a thread.
Definition: threadutility.h:127
void postThreadUnblockSignals()
A post-start thread function that unblocks signals that preThreadBlockSignals blocked before starting...
static void configure(ThreadConfigurationType type)
Utility method for configuring the ThreadUtility in a standard configuration.
std::function< void()> ThreadStartPost
Called after a thread has started.
Definition: threadutility.h:57
ThreadConfigurationType
Definition: threadutility.h:60
std::function< void(LogString threadName, std::thread::id threadId, std::thread::native_handle_type nativeHandle)> ThreadStarted
Called when a new thread has started.
Definition: threadutility.h:51
std::function< void()> ThreadStartPre
A function that will be called before a thread is started.
Definition: threadutility.h:38
std::basic_string< logchar > LogString
Definition: logstring.h:60
LOG4CXX_PTR_DEF(Appender)