forked from yuwenyong/net4cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request yuwenyong#20 from yuwenyong/dev
v3.3.0
- Loading branch information
Showing
8 changed files
with
433 additions
and
32 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
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,10 @@ | ||
// | ||
// Created by yuwenyong.vincent on 2019-01-06. | ||
// | ||
|
||
#include "net4cxx/common/threading/concurrentqueue.h" | ||
|
||
|
||
NS_BEGIN | ||
|
||
NS_END |
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,137 @@ | ||
// | ||
// Created by yuwenyong.vincent on 2019-01-06. | ||
// | ||
|
||
#ifndef NET4CXX_COMMON_THREADING_CONCURRENTQUEUE_H | ||
#define NET4CXX_COMMON_THREADING_CONCURRENTQUEUE_H | ||
|
||
#include "net4cxx/common/common.h" | ||
|
||
|
||
NS_BEGIN | ||
|
||
|
||
template <typename ValueT> | ||
class ConcurrentQueue { | ||
public: | ||
bool empty() const { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
return _queue.empty(); | ||
} | ||
|
||
size_t size() const { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
return _queue.size(); | ||
} | ||
|
||
bool enqueue(ValueT &&val) { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
if (_terminated || _stopped) { | ||
return false; | ||
} | ||
_queue.emplace_back(std::forward<ValueT>(val)); | ||
_cond.notify_one(); | ||
return true; | ||
} | ||
|
||
bool dequeue(ValueT &val) { | ||
bool res{false}; | ||
std::unique_lock<std::mutex> lock(_mut); | ||
do { | ||
if (_terminated) { | ||
break; | ||
} else if (!_queue.empty()) { | ||
val = std::move(_queue.front()); | ||
_queue.pop_front(); | ||
res = true; | ||
break; | ||
} else if (_stopped) { | ||
break; | ||
} | ||
_cond.wait(lock); | ||
} while (true); | ||
return res; | ||
} | ||
|
||
std::shared_ptr<ValueT> dequeue() { | ||
std::shared_ptr<ValueT> res; | ||
std::unique_lock<std::mutex> lock(_mut); | ||
do { | ||
if (_terminated) { | ||
break; | ||
} else if (!_queue.empty()) { | ||
res = std::make_shared<ValueT>(std::move(_queue.front())); | ||
_queue.pop_front(); | ||
break; | ||
} else if (_stopped) { | ||
break; | ||
} | ||
_cond.wait(lock); | ||
} while (true); | ||
return res; | ||
} | ||
|
||
bool tryDequeue(ValueT &val) { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
if (_terminated) { | ||
return false; | ||
} | ||
if (!_queue.empty()) { | ||
val = std::move(_queue.front()); | ||
_queue.pop_front(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
std::shared_ptr<ValueT> tryDequeue() { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
if (_terminated) { | ||
return nullptr; | ||
} | ||
if (!_queue.empty()) { | ||
auto res = std::make_shared<ValueT>(std::move(_queue.front())); | ||
_queue.pop_front(); | ||
return res; | ||
} | ||
return nullptr; | ||
} | ||
|
||
void stop() { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
_stopped = true; | ||
_cond.notify_all(); | ||
} | ||
|
||
bool stopped() const { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
return _stopped; | ||
} | ||
|
||
void terminate() { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
_terminated = true; | ||
_cond.notify_all(); | ||
} | ||
|
||
bool terminated() const { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
return _terminated; | ||
} | ||
|
||
void reset() { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
_stopped = false; | ||
_terminated = false; | ||
} | ||
protected: | ||
mutable std::mutex _mut; | ||
std::condition_variable _cond; | ||
std::deque<ValueT> _queue; | ||
bool _stopped{false}; | ||
bool _terminated{false}; | ||
}; | ||
|
||
NS_END | ||
|
||
#endif //NET4CXX_COMMON_THREADING_CONCURRENTQUEUE_H |
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,64 @@ | ||
// | ||
// Created by yuwenyong.vincent on 2019-01-06. | ||
// | ||
|
||
#include "net4cxx/common/threading/taskpool.h" | ||
|
||
|
||
NS_BEGIN | ||
|
||
bool TaskPool::start(size_t threadCount) { | ||
if (threadCount == 0) { | ||
return false; | ||
} | ||
if (!_threads.empty()) { | ||
return false; | ||
} | ||
if (_stopped || _terminated) { | ||
return false; | ||
} | ||
for (size_t i = 0; i != threadCount; ++i) { | ||
_threads.emplace_back(std::thread([this](){ | ||
process(); | ||
})); | ||
} | ||
return true; | ||
} | ||
|
||
void TaskPool::process() { | ||
std::unique_ptr<TaskBase> task; | ||
while (true) { | ||
task = pop(); | ||
if (!task) { | ||
break; | ||
} | ||
task->execute(); | ||
task.reset(); | ||
} | ||
} | ||
|
||
std::unique_ptr<TaskPool::TaskBase> TaskPool::pop() { | ||
std::unique_ptr<TaskBase> res; | ||
std::unique_lock<std::mutex> lock(_mut); | ||
do { | ||
if (_terminated) { | ||
break; | ||
} else if (!_taskList.empty()) { | ||
res.reset(&_taskList.front()); | ||
_taskList.pop_front(); | ||
break; | ||
} else if (_stopped) { | ||
break; | ||
} | ||
_cond.wait(lock); | ||
} while (true); | ||
return res; | ||
} | ||
|
||
void TaskPool::push(std::unique_ptr<TaskBase> &&task) { | ||
std::lock_guard<std::mutex> lock(_mut); | ||
_taskList.push_back(*task.release()); | ||
_cond.notify_one(); | ||
} | ||
|
||
NS_END |
Oops, something went wrong.