13 Star 69 Fork 22

johnsonyl/cpps

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
thread.cpp 1.56 KB
一键复制 编辑 原始数据 按行查看 历史
johnsonyl 提交于 2023-12-02 21:34 +08:00 . 修复多线程GC崩溃问题
enum threadstate {
pending,
running,
stoping,
stoped
};
class basethread
{
var __thread;
var state;
basethread(){
state = threadstate::pending;
__thread = null;
}
//virsual
var run(){ return false;}
//static
var _basethread_thread_func(var _thread){
if(isvalid(_thread.init)) _thread.init();
while(_thread.state == threadstate::running){
var b = _thread.run();
if(b == false) break;
}
_thread.state = threadstate::stoped;
}
var start(){
state = threadstate::running;
__thread = new thread(_basethread_thread_func,this);
}
var stop(){
if(state == threadstate::running){
state = threadstate::stoping;
__thread.join();
__thread = null;
state = threadstate::stoped;
}
}
var isrunning(){
return state == threadstate::running;
}
}
enum future_status{
ready = 0,
timeout = 1,
deferred = 2 //延迟执行的暂未实现
};
class future{
var _thread = null;
var lock = new mutex();
future(var func,... params){
_thread = new thread(func,params);
}
var wait_for(var tm){
lock.lock();
var curtm = time.gettickcount();
while(time.gettickcount() - curtm < tm){
if(_thread.isdone()) {
lock.unlock();
return future_status::ready;
}
sleep(1);
}
lock.unlock();
return future_status::timeout;
}
var wait(){
lock.lock();
_thread.join();
_thread = null;
lock.unlock();
}
var get(){
return _thread.get();
}
}
namespace std{
//std::async(
var _async(var func,... params){
return new future(func,params);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/cppscript/cpps.git
git@gitee.com:cppscript/cpps.git
cppscript
cpps
cpps
master

搜索帮助