Apache log4cxx Version 0.13.0
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 {
28namespace helpers {
29
35typedef std::function<void()> ThreadStartPre;
36
46typedef std::function<void( LogString threadName,
47 std::thread::id threadId,
48 std::thread::native_handle_type nativeHandle )> ThreadStarted;
49
54typedef std::function<void()> ThreadStartPost;
55
61};
62
63class ThreadUtility;
65
66class LOG4CXX_EXPORT ThreadUtility {
67private:
69
70 log4cxx::helpers::ThreadStartPre preStartFunction();
71 log4cxx::helpers::ThreadStarted threadStartedFunction();
72 log4cxx::helpers::ThreadStartPost postStartFunction();
73
74 struct priv_data;
75 std::unique_ptr<priv_data> m_priv;
76
77public:
79
80 static std::shared_ptr<ThreadUtility> instance();
81
87
94 ThreadStarted started,
95 ThreadStartPost post_start );
96
103
109 std::thread::id thread_id,
110 std::thread::native_handle_type native_handle);
111
118
122 template<class Function, class... Args>
123 std::thread createThread(LogString name,
124 Function&& f,
125 Args&&... args){
126 log4cxx::helpers::ThreadStartPre pre_start = preStartFunction();
127 log4cxx::helpers::ThreadStarted thread_start = threadStartedFunction();
128 log4cxx::helpers::ThreadStartPost post_start = postStartFunction();
129
130 if( pre_start ){
131 pre_start();
132 }
133 std::thread t( f, args... );
134 if( thread_start ){
135 thread_start( name,
136 t.get_id(),
137 t.native_handle() );
138 }
139 if( post_start ){
140 post_start();
141 }
142 return t;
143 }
144};
145
146} /* namespace helpers */
147} /* namespace log4cxx */
148
149#endif
Definition: threadutility.h:66
static std::shared_ptr< ThreadUtility > instance()
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.
std::thread createThread(LogString name, Function &&f, Args &&... args)
Start a thread.
Definition: threadutility.h:123
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.
LOG4CXX_PTR_DEF(AppenderAttachableImpl)
std::function< void()> ThreadStartPost
Called after a thread has started.
Definition: threadutility.h:54
ThreadConfigurationType
Definition: threadutility.h:56
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:48
std::function< void()> ThreadStartPre
A function that will be called before a thread is started.
Definition: threadutility.h:35
Definition: messagehandler.h:23
std::basic_string< logchar > LogString
Definition: logstring.h:66