Skip to content

Commit

Permalink
broke down threadpool.cpp and threadpool.h into multiple files, each …
Browse files Browse the repository at this point in the history
…file coresponds to one class now
  • Loading branch information
aberon295 committed Sep 17, 2015
1 parent b28fc25 commit 69f168f
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 123 deletions.
17 changes: 17 additions & 0 deletions CondVar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "CondVar.h"

CondVar::CondVar() {
pthread_cond_init(&m_cond_var, NULL);
}
CondVar::~CondVar() {
pthread_cond_destroy(&m_cond_var);
}
void CondVar::wait(pthread_mutex_t* mutex) {
pthread_cond_wait(&m_cond_var, mutex);
}
void CondVar::signal() {
pthread_cond_signal(&m_cond_var);
}
void CondVar::broadcast() {
pthread_cond_broadcast(&m_cond_var);
}
24 changes: 24 additions & 0 deletions CondVar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <pthread.h>
#include <unistd.h>
#include <deque>
#include <iostream>
#include <vector>

#include "Global.h"

using namespace std;

class CondVar {
public:

CondVar();
~CondVar();
void wait(pthread_mutex_t* mutex);
void signal();
void broadcast();

private:
pthread_cond_t m_cond_var;
};
5 changes: 5 additions & 0 deletions Global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

const int DEFAULT_POOL_SIZE = 10;
const int STARTED = 0;
const int STOPPED = 1;
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
OBJPATH=bin/obj
EXAMPLEPATH=bin/example

all:
g++ threadpool.cpp -lpthread -fpic -c -o bin/obj/threadpool.o
g++ -L./bin bin/obj/threadpool.o -lpthread threadpool_test.cpp -o bin/example/threadpool_test
g++ CondVar.cpp -lpthread -c -o $(OBJPATH)/CondVar.o
g++ Mutex.cpp -lpthread -c -o $(OBJPATH)/Mutex.o
g++ Task.cpp -lpthread -c -o $(OBJPATH)/Task.o
g++ ThreadPool.cpp -lpthread -c -o $(OBJPATH)/ThreadPool.o
g++ $(OBJPATH)/CondVar.o $(OBJPATH)/Mutex.o $(OBJPATH)/Task.o $(OBJPATH)/ThreadPool.o threadpool_test.cpp -lpthread -o $(EXAMPLEPATH)threadpool_test

#all:
# g++ threadpool.cpp -lpthread -fpic -c -o bin/obj/threadpool.o
# g++ -L./bin bin/obj/threadpool.o -lpthread threadpool_test.cpp -o bin/example/threadpool_test

#threadpool:
# g++ threadpool.cpp -lpthread -fpic -c -o bin/obj/threadpool.o
Expand Down
26 changes: 26 additions & 0 deletions Mutex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "Mutex.h"

Mutex::Mutex() {
pthread_mutex_init(&m_lock, NULL);
is_locked = false;
}

Mutex::~Mutex() {
while(is_locked);
unlock(); // Unlock Mutex after shared resource is safe
pthread_mutex_destroy(&m_lock);
}

void Mutex::lock() {
pthread_mutex_lock(&m_lock);
is_locked = true;
}

void Mutex::unlock() {
is_locked = false; // do it BEFORE unlocking to avoid race condition
pthread_mutex_unlock(&m_lock);
}

pthread_mutex_t* Mutex::get_mutex_ptr(){
return &m_lock;
}
27 changes: 27 additions & 0 deletions Mutex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <pthread.h>
#include <unistd.h>
#include <deque>
#include <iostream>
#include <vector>
#include <errno.h>
#include <string.h>

#include "Global.h"

using namespace std;

class Mutex
{
public:
Mutex();
~Mutex();
void lock();
void unlock();
pthread_mutex_t* get_mutex_ptr();

private:
pthread_mutex_t m_lock;
volatile bool is_locked;
};
18 changes: 18 additions & 0 deletions Task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "Task.h"

Task::Task(void (*fn_ptr)(void*), void* arg) : m_fn_ptr(fn_ptr), m_arg(arg) {
}

Task::~Task() {
}

void Task::operator()() {
(*m_fn_ptr)(m_arg);
if (m_arg != NULL) {
delete m_arg;
}
}

void Task::run() {
(*m_fn_ptr)(m_arg);
}
28 changes: 28 additions & 0 deletions Task.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <pthread.h>
#include <unistd.h>
#include <deque>
#include <iostream>
#include <vector>
#include <errno.h>
#include <string.h>

#include "Global.h"

using namespace std;

//template<class TClass>
class Task
{
public:
// Task(TCLass::* obj_fn_ptr); // pass an object method pointer
Task(void (*fn_ptr)(void*), void* arg); // pass a free function pointer
~Task();
void operator()();
void run();
private:
// TClass* _obj_fn_ptr;
void (*m_fn_ptr)(void*);
void* m_arg;
};
26 changes: 1 addition & 25 deletions threadpool.cpp → ThreadPool.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
#include "threadpool.h"

#include <errno.h>
#include <string.h>

Task::Task(void (*fn_ptr)(void*), void* arg) : m_fn_ptr(fn_ptr), m_arg(arg)
{
}

Task::~Task()
{
}

void Task::operator()()
{
(*m_fn_ptr)(m_arg);
if (m_arg != NULL) {
delete m_arg;
}
}

void Task::run()
{
(*m_fn_ptr)(m_arg);
}
#include "ThreadPool.h"

ThreadPool::ThreadPool() : m_pool_size(DEFAULT_POOL_SIZE)
{
Expand Down
37 changes: 37 additions & 0 deletions ThreadPool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <pthread.h>
#include <unistd.h>
#include <deque>
#include <iostream>
#include <vector>
#include <errno.h>
#include <string.h>

#include "Mutex.h"
#include "Task.h"
#include "CondVar.h"
#include "Global.h"

using namespace std;


class ThreadPool
{
public:
ThreadPool();
ThreadPool(int pool_size);
~ThreadPool();
int initialize_threadpool();
int destroy_threadpool();
void* execute_thread();
int add_task(Task* task);
private:
int m_pool_size;
Mutex m_task_mutex;
CondVar m_task_cond_var;
std::vector<pthread_t> m_threads; // storage for threads
std::deque<Task*> m_tasks;
volatile int m_pool_state;
};

Binary file added bin/examplethreadpool_test
Binary file not shown.
Binary file removed bin/obj/threadpool.o
Binary file not shown.
95 changes: 0 additions & 95 deletions threadpool.h

This file was deleted.

4 changes: 3 additions & 1 deletion threadpool_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "threadpool.h"
#include "ThreadPool.h"

//#include "threadpool.h"

#include <iostream>

Expand Down

0 comments on commit 69f168f

Please sign in to comment.