Skip to content

Commit 2c549ad

Browse files
committed
Change mutex as ThreadPool member
1 parent 927cf2f commit 2c549ad

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

threadpool.cpp

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@
1515
You should have received a copy of the GNU General Public License
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
18-
#include <algorithm>
19-
#include <stdlib.h>
2018
#include "threadpool.h"
19+
#include <stdlib.h>
20+
#include <errno.h>
2121

2222
using namespace std;
2323

24-
pthread_mutex_t ThreadPool::_mutex_sync = PTHREAD_MUTEX_INITIALIZER;
25-
pthread_mutex_t ThreadPool::_mutex_work_completion = PTHREAD_MUTEX_INITIALIZER;
26-
2724

2825
ThreadPool::ThreadPool(unsigned int num_thread)
2926
:_thread_pool(num_thread)
3027
,_num_thread(num_thread)
3128
,_worker_queue(num_thread, NULL)
3229
,_queue_size(num_thread)
3330
{
34-
pthread_mutex_lock(&_mutex_sync);
3531
_top_index = 0;
3632
_bottom_index = 0;
3733
_incomplete_work = 0;
3834
sem_init(&_available_work, 0, 0);
3935
sem_init(&_available_thread, 0, _queue_size);
40-
pthread_mutex_unlock(&_mutex_sync);
36+
37+
init_mutex(&_mutex_sync);
38+
init_mutex(&_mutex_work_completion);
4139

4240
for (std::vector<pthread_t>::iterator i = _thread_pool.begin(); i != _thread_pool.end(); ++i) {
4341
pthread_create(&*i, NULL, &ThreadPool::thread_execute, this);
@@ -123,3 +121,38 @@ void *ThreadPool::thread_execute(void *param)
123121
}
124122
return 0;
125123
}
124+
125+
void ThreadPool::init_mutex(pthread_mutex_t* const mutex)
126+
{
127+
int ret = pthread_mutex_init(mutex, NULL);
128+
switch (ret) {
129+
case 0:
130+
break;
131+
case EAGAIN:
132+
throw Error("EAGAIN returned by pthread_mutex_init()");
133+
case ENOMEM:
134+
throw Error("ENOMEM returned by pthread_mutex_init()");
135+
case EPERM:
136+
throw Error("EPERM returned by pthread_mutex_init()");
137+
case EBUSY:
138+
throw Error("EBUSY returned by pthread_mutex_init()");
139+
case EINVAL:
140+
throw Error("EINVAL returned by pthread_mutex_init()");
141+
default:
142+
throw Error("UNKNOWN returned by pthread_mutex_init()");
143+
};
144+
}
145+
146+
Error::Error(const char * what)
147+
:_what(what)
148+
{
149+
}
150+
151+
Error::~Error() throw()
152+
{
153+
}
154+
155+
const char* Error::what() const throw()
156+
{
157+
return _what;
158+
}

threadpool.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,22 @@
1616
along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19+
#include <exception>
20+
1921
#include <pthread.h>
2022
#include <semaphore.h>
2123
#include <iostream>
2224
#include <vector>
2325

26+
class Error: public std::exception {
27+
public:
28+
explicit Error(const char * what);
29+
virtual ~Error() throw();
30+
virtual const char* what() const throw();
31+
private:
32+
const char * _what;
33+
};
34+
2435
/*
2536
WorkerThread class
2637
This class needs to be sobclassed by the user.
@@ -53,14 +64,16 @@ class ThreadPool{
5364

5465
static void *thread_execute(void *param);
5566

56-
static pthread_mutex_t _mutex_sync;
57-
static pthread_mutex_t _mutex_work_completion;
5867

5968

6069
private:
70+
static void init_mutex(pthread_mutex_t* mutex);
6171
std::vector<pthread_t> _thread_pool;
6272
unsigned int _num_thread;
6373

74+
pthread_mutex_t _mutex_sync;
75+
pthread_mutex_t _mutex_work_completion;
76+
6477
sem_t _available_work;
6578
sem_t _available_thread;
6679

0 commit comments

Comments
 (0)