-
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.
- Loading branch information
fs
committed
Mar 7, 2024
1 parent
8b175c6
commit d58a516
Showing
4 changed files
with
164 additions
and
1 deletion.
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,49 @@ | ||
#include "work_thread.h" | ||
#include "work_queue.h" | ||
#include "task.h" | ||
|
||
#include <iostream> | ||
|
||
namespace async_framework | ||
{ | ||
std::thread::id WorkThread::_s_main_thread_id; | ||
|
||
void WorkThread::main_loop() { | ||
while (_running) { | ||
process_one_task(true); | ||
} | ||
} | ||
|
||
bool WorkThread::process_one_task(bool wait) { | ||
std::shared_ptr<Task> task; | ||
if (!wait) { | ||
//非阻塞 | ||
task = _queue->try_deque( ); | ||
if (task == nullptr) { | ||
return false; | ||
} | ||
} else { | ||
// 获取任务,如果没有任务,则阻塞当前线程 | ||
task = _queue->dequeue(); | ||
} | ||
|
||
if (task == nullptr) { | ||
return false; | ||
} | ||
bool immediate = true; | ||
task->execute(immediate); | ||
return true; | ||
} | ||
|
||
void WorkThread::start(bool new_os_thread) { | ||
_running .store(true); | ||
if (new_os_thread) { | ||
_thread = std::make_shared<std::thread>(&WorkThread::main_loop, this); | ||
auto native_thread = _thread->native_handle(); | ||
pthread_setname_np(native_thread, _name.c_str()); | ||
} else { | ||
main_loop(); | ||
} | ||
} | ||
|
||
} // namespace async_framework |
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,50 @@ | ||
#ifndef WORK_THREAD_H | ||
#define WORK_THREAD_H | ||
|
||
#include <string> | ||
#include <memory> | ||
#include <atomic> | ||
#include <thread> | ||
|
||
#include "utils/utils_define.h" | ||
#include "task.h" | ||
#include "task_queue.h" | ||
|
||
namespace async_framework { | ||
|
||
class WorkThread { | ||
public: | ||
WorkThread(const std::string &name, const std::shared_ptr<TaskQueue>& queue) | ||
: _name(name) | ||
, _queue(queue){}; | ||
|
||
virtual ~WorkThread(); | ||
void start(bool new_os_thread = true); | ||
|
||
// 判断当前正在执行的线程是否是主线程 | ||
static bool is_main_thread() { | ||
return _s_main_thread_id == std::this_thread::get_id(); | ||
} | ||
|
||
static void set_main_thread_id(const std::thread::id& main_thread_id) { | ||
_s_main_thread_id = main_thread_id; | ||
} | ||
protected: | ||
// 从队列中获取一个任务并处理,返回是否获取到任务,wait表示如果没有任务是否阻塞当前线程 | ||
virtual bool process_one_task(bool wait = false); | ||
|
||
// 线程的主循环, 默认实现为如果队列为空,则阻塞,否则处理任务 | ||
virtual void main_loop(); | ||
protected: | ||
std::string _name; | ||
std::shared_ptr<TaskQueue> _queue; | ||
std::atomic<bool> _running; | ||
std::shared_ptr<std::thread> _thread; // | ||
static std::thread::id _s_main_thread_id; // _s_main_thread_id 所有对象共享,记录主线程id | ||
private: | ||
DISALLOW_COPY_AND_ASSIGN(WorkThread); | ||
}; | ||
|
||
} | ||
|
||
#endif |