-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
broke down threadpool.cpp and threadpool.h into multiple files, each …
…file coresponds to one class now
- Loading branch information
Showing
14 changed files
with
198 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|