Apache Log4cxx Version 1.1.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
27namespace log4cxx
28{
29namespace helpers
30{
31
37typedef std::function<void()> ThreadStartPre;
38
48typedef std::function<void( LogString threadName,
49 std::thread::id threadId,
50 std::thread::native_handle_type nativeHandle )> ThreadStarted;
51
56typedef std::function<void()> ThreadStartPost;
57
59{
64};
65
66class ThreadUtility;
68
69class LOG4CXX_EXPORT ThreadUtility
70{
71 private:
73
74 log4cxx::helpers::ThreadStartPre preStartFunction();
75 log4cxx::helpers::ThreadStarted threadStartedFunction();
76 log4cxx::helpers::ThreadStartPost postStartFunction();
77
78 LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(priv_data, m_priv)
79 public:
81
83
89
96 ThreadStarted started,
97 ThreadStartPost post_start );
98
105
111 std::thread::id thread_id,
112 std::thread::native_handle_type native_handle);
113
120
124 template<class Function, class... Args>
125 std::thread createThread(LogString name,
126 Function&& f,
127 Args&& ... args)
128 {
129 log4cxx::helpers::ThreadStartPre pre_start = preStartFunction();
130 log4cxx::helpers::ThreadStarted thread_start = threadStartedFunction();
131 log4cxx::helpers::ThreadStartPost post_start = postStartFunction();
132
133 if ( pre_start )
134 {
135 pre_start();
136 }
137
138 std::thread t( f, args... );
139
140 if ( thread_start )
141 {
142 thread_start( name,
143 t.get_id(),
144 t.native_handle() );
145 }
146
147 if ( post_start )
148 {
149 post_start();
150 }
151
152 return t;
153 }
154};
155
156} /* namespace helpers */
157} /* namespace log4cxx */
158
159#endif
Definition: threadutility.h:70
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:125
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:56
LOG4CXX_PTR_DEF(Object)
ThreadConfigurationType
Definition: threadutility.h:59
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:50
std::function< void()> ThreadStartPre
A function that will be called before a thread is started.
Definition: threadutility.h:37
Definition: configuration.h:25
std::basic_string< logchar > LogString
Definition: logstring.h:60