C++?????????????
???????????? ???????[ 2013/3/25 13:45:10 ] ????????
????Task?????????run????????б???????????????????????task_impl_i??????У?????????????????????????????????task_impl_i??????????????丳?費(fèi)??????????????ɡ?????????????????????????????????????????????ò???????task???????????task_impl_i????????????????????????????????????task_impl_i??????????????Щ??????????????????????в??????????????????????task????task_binder_t ?????е?gen???????????????????????????????task_t????
struct task_binder_t
{
//! C function
static task_t gen(void (*func_)(void*)?? void* p_)
{
return task_t(func_?? p_);
}
template<typename RET>
static task_t gen(RET (*func_)(void))
{
struct lambda_t
{
static void task_func(void* p_)
{
(*(RET(*)(void))p_)();
};
};
return task_t(lambda_t::task_func?? (void*)func_);
}
template<typename FUNCT?? typename ARG1>
static task_t gen(FUNCT func_?? ARG1 arg1_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
lambda_t(FUNCT func_?? const ARG1& arg1_):
dest_func(func_)??
arg1(arg1_)
{}
virtual void run()
{
(*dest_func)(arg1);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func?? arg1);
}
};
return task_t(new lambda_t(func_?? arg1_));
????????????
??????????????????????????????????????????????????????????????????????task_t??????????????????????С?????????????????????????????????????????????????????????????????????????????????£?
?????? ????????????task_t????
?????? ?????????????????У???task_t ??????????β?????????????????????wait???????????????????signal??????????
?????????????????£?
void produce(const task_t& task_)
{
lock_guard_t lock(m_mutex);
bool need_sig = m_tasklist.empty();
m_tasklist.push_back(task_);
if (need_sig)
{
m_cond.signal();
}
}
????????????
?????????????????????????????????????????????????????????е??????????????????????????????????????????????????????????????????????λ?????????????£?
int consume(task_t& task_)
{
lock_guard_t lock(m_mutex);
while (m_tasklist.empty())
{
if (false == m_flag)
{
return -1;
}
m_cond.wait();
}
task_ = m_tasklist.front();
m_tasklist.pop_front();
return 0;
}
int run()
{
task_t t;
while (0 == consume(t))
{
t.run();
}
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11