Skyld AV  0.6
On access virus scanning for Linux
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ThreadPool.h
Go to the documentation of this file.
1 /*
2  * File: ThreadPool.h
3  *
4  * Copyright 2013 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
23 #ifndef THREADPOOL_H
24 #define THREADPOOL_H
25 
26 #include <deque>
27 #include <pthread.h>
28 
35 class ThreadPool {
36 public:
37  enum status {
40  };
41 
42  ThreadPool(int nThreads, void* (*workRoutine) (void *));
43  void add(void *workItem);
44  void *getWorkItem();
45  long getWorklistSize();
46  virtual ~ThreadPool();
47 private:
48  enum status status;
49  int createThread();
50  void exitThread(void *retval);
51  int isStopping();
52  pthread_cond_t cond;
53  static void *worker (void *);
54  pthread_mutex_t mutexThread;
55  pthread_mutex_t mutexWorker;
56  pthread_mutex_t mutexWorkItem;
58  std::deque<void *> worklist;
59  void* (*workRoutine) (void *);
60 };
61 
62 #endif /* THREADPOOL_H */
63 
pthread_mutex_t mutexThread
Definition: ThreadPool.h:54
void *(* workRoutine)(void *)
Definition: ThreadPool.h:59
static void * worker(void *)
Working thread.
Definition: ThreadPool.cc:144
pthread_mutex_t mutexWorker
Definition: ThreadPool.h:55
pthread_mutex_t mutexWorkItem
Definition: ThreadPool.h:56
ThreadPool(int nThreads, void *(*workRoutine)(void *))
Creates a new thread pool.
Definition: ThreadPool.cc:33
virtual ~ThreadPool()
Deletes thread pool.
Definition: ThreadPool.cc:172
std::deque< void * > worklist
Definition: ThreadPool.h:58
Implements the thread pool pattern.
Definition: ThreadPool.h:35
int isStopping()
Is thread pool stopping.
Definition: ThreadPool.cc:134
long getWorklistSize()
Gets size of worklist.
Definition: ThreadPool.cc:119
void exitThread(void *retval)
Exits a worker thread.
Definition: ThreadPool.cc:87
void * getWorkItem()
Gets a work item.
Definition: ThreadPool.cc:100
int createThread()
Creates a new worker thread.
Definition: ThreadPool.cc:67
void add(void *workItem)
Adds a work item to the work list.
Definition: ThreadPool.cc:55
pthread_cond_t cond
Definition: ThreadPool.h:52
int thread_count
Definition: ThreadPool.h:57