学习下知然网友写的taskqueue
博主在他的博客里对taskqueue的各种使用情况和使用方法都介绍的很清楚:http://www.cnblogs.com/zhiranok/archive/2013/01/14/task_queue.html
先把代码贴上,之后研究:
#ifndef _TASK_QUEUE_I_
#define _TASK_QUEUE_I_ #include <list>
#include <vector>
using namespace std; namespace ff { typedef void (*task_func_t)(void*); class task_impl_i
{
public:
virtual ~task_impl_i(){}
virtual void run() = 0;
virtual task_impl_i* fork() = 0;
}; class task_impl_t: public task_impl_i
{
public:
task_impl_t(task_func_t func_, void* arg_):
m_func(func_),
m_arg(arg_)
{} virtual void run()
{
m_func(m_arg);
} virtual task_impl_i* fork()
{
return new task_impl_t(m_func, m_arg);
} protected:
task_func_t m_func;
void* m_arg;
}; struct task_t
{
static void dumy(void*){}
task_t(task_func_t f_, void* d_):
task_impl(new task_impl_t(f_, d_))
{
}
task_t(task_impl_i* task_imp_):
task_impl(task_imp_)
{
}
task_t(const task_t& src_):
task_impl(src_.task_impl->fork())
{
}
task_t()
{
task_impl = new task_impl_t(&task_t::dumy, NULL);
}
~task_t()
{
delete task_impl;
}
task_t& operator=(const task_t& src_)
{
delete task_impl;
task_impl = src_.task_impl->fork();
return *this;
} void run()
{
task_impl->run();
}
task_impl_i* task_impl;
}; class task_queue_i
{
public:
typedef list<task_t> task_list_t;
public:
virtual ~task_queue_i(){}
virtual void close() = 0;
virtual void produce(const task_t& task_) =0;
virtual void multi_produce(const task_list_t& task_) =0;
virtual int consume(task_t& task_) = 0;
virtual int consume_all(task_list_t&) = 0;
virtual int run() = 0;
virtual int batch_run() = 0;
}; 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_));
}
template<typename FUNCT, typename ARG1, typename ARG2>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_)
{
struct lambda_t:public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_, ARG5 arg5_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
lambda_t(FUNCT func_,
const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6,
typename ARG7>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_, ARG7 arg7_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6,
typename ARG7, typename ARG8>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
lambda_t(FUNCT func_,
const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, const ARG8& arg8_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6,
typename ARG7, typename ARG8, typename ARG9>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_, ARG9 arg9_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
ARG9 arg9;
lambda_t(FUNCT func_,
const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, const ARG8& arg8_, const ARG9& arg9_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_),
arg9(arg9_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_, arg9_));
}
//! class fuctions
template<typename T, typename RET>
static task_t gen(RET (T::*func_)(void), T* obj_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(void);
T* obj;
lambda_t(RET (T::*func_)(void), T* obj_):
dest_func(func_),
obj(obj_)
{}
virtual void run()
{
(obj->*dest_func)();
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj);
}
};
return task_t(new lambda_t(func_, obj_));
}
template<typename T, typename RET, typename FARG1, typename ARG1>
static task_t gen(RET (T::*func_)(FARG1), T* obj_, ARG1 arg1_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1);
T* obj;
ARG1 arg1;
lambda_t(RET (T::*func_)(FARG1), T* obj_, const ARG1& arg1_):
dest_func(func_),
obj(obj_),
arg1(arg1_)
{}
virtual void run()
{
(obj->*dest_func)(arg1);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1);
}
};
return task_t(new lambda_t(func_, obj_, arg1_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename ARG1, typename ARG2>
static task_t gen(RET (T::*func_)(FARG1, FARG2), T* obj_, ARG1 arg1_, ARG2 arg2_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2);
T* obj;
ARG1 arg1;
ARG2 arg2;
lambda_t(RET (T::*func_)(FARG1, FARG2), T* obj_, const ARG1& arg1_, const ARG2& arg2_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename ARG1, typename ARG2,
typename ARG3>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3), T* obj_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3), T* obj_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4,
typename ARG1, typename ARG2, typename ARG3, typename ARG4>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4), T* obj_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_, ARG5 arg5_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename FARG7,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_, ARG7 arg7_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename FARG7, typename FARG8,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7, typename ARG8>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, const ARG8& arg8_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename FARG7, typename FARG8, typename FARG9,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8, typename ARG9>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_, ARG9 arg9_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
ARG9 arg9;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9), T* obj_,
const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_,
const ARG8& arg8_, const ARG9& arg9_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_),
arg9(arg9_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_, arg9_));
}
}; } #endif
#ifndef _TASK_QUEUE_IMPL_H_
#define _TASK_QUEUE_IMPL_H_ #include<pthread.h>
#include <list>
#include <stdexcept>
using namespace std; #include "task_queue_i.h"
#include "lock.h" namespace ff {
#ifndef _TASK_QUEUE_I_
#define _TASK_QUEUE_I_ #include <list>
#include <vector>
using namespace std; namespace ff { typedef void (*task_func_t)(void*); class task_impl_i
{
public:
virtual ~task_impl_i(){}
virtual void run() = 0;
virtual task_impl_i* fork() = 0;
}; class task_impl_t: public task_impl_i
{
public:
task_impl_t(task_func_t func_, void* arg_):
m_func(func_),
m_arg(arg_)
{} virtual void run()
{
m_func(m_arg);
} virtual task_impl_i* fork()
{
return new task_impl_t(m_func, m_arg);
} protected:
task_func_t m_func;
void* m_arg;
}; struct task_t
{
static void dumy(void*){}
task_t(task_func_t f_, void* d_):
task_impl(new task_impl_t(f_, d_))
{
}
task_t(task_impl_i* task_imp_):
task_impl(task_imp_)
{
}
task_t(const task_t& src_):
task_impl(src_.task_impl->fork())
{
}
task_t()
{
task_impl = new task_impl_t(&task_t::dumy, NULL);
}
~task_t()
{
delete task_impl;
}
task_t& operator=(const task_t& src_)
{
delete task_impl;
task_impl = src_.task_impl->fork();
return *this;
} void run()
{
task_impl->run();
}
task_impl_i* task_impl;
}; class task_queue_i
{
public:
typedef list<task_t> task_list_t;
public:
virtual ~task_queue_i(){}
virtual void close() = 0;
virtual void produce(const task_t& task_) =0;
virtual void multi_produce(const task_list_t& task_) =0;
virtual int consume(task_t& task_) = 0;
virtual int consume_all(task_list_t&) = 0;
virtual int run() = 0;
virtual int batch_run() = 0;
}; 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_));
}
template<typename FUNCT, typename ARG1, typename ARG2>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_)
{
struct lambda_t:public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
static task_t gen(FUNCT func_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_, ARG5 arg5_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
lambda_t(FUNCT func_,
const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6,
typename ARG7>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_, ARG7 arg7_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
lambda_t(FUNCT func_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6,
typename ARG7, typename ARG8>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
lambda_t(FUNCT func_,
const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, const ARG8& arg8_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_));
}
template<typename FUNCT, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6,
typename ARG7, typename ARG8, typename ARG9>
static task_t gen(FUNCT func_,
ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_,
ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_, ARG9 arg9_)
{
struct lambda_t: public task_impl_i
{
FUNCT dest_func;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
ARG9 arg9;
lambda_t(FUNCT func_,
const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, const ARG8& arg8_, const ARG9& arg9_):
dest_func(func_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_),
arg9(arg9_)
{}
virtual void run()
{
(*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
};
return task_t(new lambda_t(func_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_, arg9_));
}
//! class fuctions
template<typename T, typename RET>
static task_t gen(RET (T::*func_)(void), T* obj_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(void);
T* obj;
lambda_t(RET (T::*func_)(void), T* obj_):
dest_func(func_),
obj(obj_)
{}
virtual void run()
{
(obj->*dest_func)();
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj);
}
};
return task_t(new lambda_t(func_, obj_));
}
template<typename T, typename RET, typename FARG1, typename ARG1>
static task_t gen(RET (T::*func_)(FARG1), T* obj_, ARG1 arg1_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1);
T* obj;
ARG1 arg1;
lambda_t(RET (T::*func_)(FARG1), T* obj_, const ARG1& arg1_):
dest_func(func_),
obj(obj_),
arg1(arg1_)
{}
virtual void run()
{
(obj->*dest_func)(arg1);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1);
}
};
return task_t(new lambda_t(func_, obj_, arg1_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename ARG1, typename ARG2>
static task_t gen(RET (T::*func_)(FARG1, FARG2), T* obj_, ARG1 arg1_, ARG2 arg2_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2);
T* obj;
ARG1 arg1;
ARG2 arg2;
lambda_t(RET (T::*func_)(FARG1, FARG2), T* obj_, const ARG1& arg1_, const ARG2& arg2_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename ARG1, typename ARG2,
typename ARG3>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3), T* obj_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3), T* obj_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4,
typename ARG1, typename ARG2, typename ARG3, typename ARG4>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4), T* obj_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_)
{
struct lambda_t:public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj_, ARG1 arg1_, ARG2 arg2_, ARG3 arg3_, ARG4 arg4_, ARG5 arg5_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5), T* obj_, const ARG1& arg1_, const ARG2& arg2_, const ARG3& arg3_, const ARG4& arg4_,
const ARG5& arg5_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename FARG7,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_, ARG7 arg7_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename FARG7, typename FARG8,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7, typename ARG8>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8), T* obj_, const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_, const ARG8& arg8_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_));
}
template<typename T, typename RET, typename FARG1, typename FARG2, typename FARG3, typename FARG4, typename FARG5,
typename FARG6, typename FARG7, typename FARG8, typename FARG9,
typename ARG1, typename ARG2, typename ARG3, typename ARG4, typename ARG5, typename ARG6, typename ARG7,
typename ARG8, typename ARG9>
static task_t gen(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9), T* obj_, ARG1 arg1_, ARG2 arg2_,
ARG3 arg3_, ARG4 arg4_, ARG5 arg5_, ARG6 arg6_, ARG7 arg7_, ARG8 arg8_, ARG9 arg9_)
{
struct lambda_t: public task_impl_i
{
RET (T::*dest_func)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9);
T* obj;
ARG1 arg1;
ARG2 arg2;
ARG3 arg3;
ARG4 arg4;
ARG5 arg5;
ARG6 arg6;
ARG7 arg7;
ARG8 arg8;
ARG9 arg9;
lambda_t(RET (T::*func_)(FARG1, FARG2, FARG3, FARG4, FARG5, FARG6, FARG7, FARG8, FARG9), T* obj_,
const ARG1& arg1_, const ARG2& arg2_,
const ARG3& arg3_, const ARG4& arg4_, const ARG5& arg5_, const ARG6& arg6_, const ARG7& arg7_,
const ARG8& arg8_, const ARG9& arg9_):
dest_func(func_),
obj(obj_),
arg1(arg1_),
arg2(arg2_),
arg3(arg3_),
arg4(arg4_),
arg5(arg5_),
arg6(arg6_),
arg7(arg7_),
arg8(arg8_),
arg9(arg9_)
{}
virtual void run()
{
(obj->*dest_func)(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
virtual task_impl_i* fork()
{
return new lambda_t(dest_func, obj, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
}
};
return task_t(new lambda_t(func_, obj_, arg1_, arg2_, arg3_, arg4_, arg5_, arg6_, arg7_, arg8_, arg9_));
}
}; } #endif
class task_queue_t: public task_queue_i
{
public:
task_queue_t():
m_flag(true),
m_cond(m_mutex)
{
}
~task_queue_t()
{
}
void close()
{
lock_guard_t lock(m_mutex);
m_flag = false;
m_cond.broadcast();
} void multi_produce(const task_list_t& task_)
{
lock_guard_t lock(m_mutex);
bool need_sig = m_tasklist.empty(); for(task_list_t::const_iterator it = task_.begin(); it != task_.end(); ++it)
{
m_tasklist.push_back(*it);
} if (need_sig)
{
m_cond.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;
} int consume_all(task_list_t& tasks_)
{
lock_guard_t lock(m_mutex); while (m_tasklist.empty())
{
if (false == m_flag)
{
return -1;
}
m_cond.wait();
} tasks_ = m_tasklist;
m_tasklist.clear(); return 0;
} int batch_run()
{
task_list_t tasks;
int ret = consume_all(tasks);
while (0 == ret)
{
for (task_list_t::iterator it = tasks.begin(); it != tasks.end(); ++it)
{
(*it).run();
}
tasks.clear();
ret = consume_all(tasks);
}
return 0;
}
private:
volatile bool m_flag;
task_list_t m_tasklist;
mutex_t m_mutex;
condition_var_t m_cond;
}; class task_queue_pool_t
{
typedef task_queue_i::task_list_t task_list_t;
typedef vector<task_queue_t*> task_queue_vt_t;
static void task_func(void* pd_)
{
task_queue_pool_t* t = (task_queue_pool_t*)pd_;
t->run();
}
public:
static task_t gen_task(task_queue_pool_t* p)
{
return task_t(&task_func, p);
}
public:
task_queue_pool_t(int n):
m_index(0)
{
for (int i = 0; i < n; ++i)
{
task_queue_t* p = new task_queue_t();
m_tqs.push_back(p);
}
} void run()
{
task_queue_t* p = NULL;
{
lock_guard_t lock(m_mutex);
if (m_index >= (int)m_tqs.size())
{
throw runtime_error("too more thread running!!");
}
p = m_tqs[m_index++];
} p->batch_run();
} ~task_queue_pool_t()
{
task_queue_vt_t::iterator it = m_tqs.begin();
for (; it != m_tqs.end(); ++it)
{
delete (*it);
}
m_tqs.clear();
} void close()
{
task_queue_vt_t::iterator it = m_tqs.begin();
for (; it != m_tqs.end(); ++it)
{
(*it)->close();
}
} size_t size() const { return m_tqs.size(); } task_queue_i* alloc(long id_)
{
return m_tqs[id_ % m_tqs.size()];
}
task_queue_i* rand_alloc()
{
static unsigned long id_ = 0;
return m_tqs[++id_ % m_tqs.size()];
}
private:
mutex_t m_mutex;
task_queue_vt_t m_tqs;
int m_index;
}; } #endif
学习下知然网友写的taskqueue的更多相关文章
- TCP/IP详解学习笔记 这位仁兄写得太好了.(转载)
TCP/IP详解学习笔记 这位仁兄写得太好了 TCP/IP详解学习笔记 这位仁兄写得太好了. http://blog.csdn.net/goodboy1881/category/20444 ...
- 前端一面/面试常考题1-页面布局:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应。
题目:假设高度已知,请写出三栏布局,其中左栏.右栏宽度各为300px,中间自适应. [题外话:日常宣读我的目标===想要成为一名优雅的程序媛] 一.分析 1. 题目真的像我们想得这么简单吗? 其实不然 ...
- 一个网友写的栈,问为啥不能迭代。具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网
一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器-七七巴巴黄页网 一个网友写的栈,问为啥不能迭代.具有__iter__ 和next方法的对象叫迭代器 python视频 ...
- 学习下新塘M0芯片的下载方法
编程方式多种多样,解释这几种方式的原理,方便做后续的回答: 一.脱机 脱机的意思就是脱离PC机,有很多芯片必须连接PC才能烧录,比如某些FPGA芯片.MCU芯片.NAND Flash芯片等.脱机和在线 ...
- 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识
深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...
- 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...
- 学习 Doug Lea 大神写的——Scalable IO in Java
学习 Doug Lea 大神写的--Scalable IO in Java 网络服务 Web services.分布式对象等等都具有相同的处理结构 Read request Decode reques ...
- 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
- 在windows下用C语言写socket通讯实例
原文:在windows下用C语言写socket通讯实例 From:Microsoft Dev Center #undef UNICODE #define WIN32_LEAN_AND_MEAN #in ...
随机推荐
- 【C++ Primer 第11章 练习答案】2. 关联容器操作
11.3.1节练习 [练习11.16]代码 map<int, int> m; auto iter = m.begin(); iter ->second = ;
- 浅谈Spring MVC知识
关于MVC框架,我相信大家都不陌生,都会说也就是模型-视图-控制器这三层的框架结构,如果你参加面试的时候考官会问:“MVC框架是什么?你说一说.”其实我们都知道这个问题还需要问的,只要你是一个开发人员 ...
- 详解webpack中的hash、chunkhash、contenthash区别
hash.chunkhash.contenthash hash一般是结合CDN缓存来使用,通过webpack构建之后,生成对应文件名自动带上对应的MD5值.如果文件内容改变的话,那么对应文件哈希值也会 ...
- Codeforces Round #467 (Div. 2) E -Lock Puzzle
Lock Puzzle 题目大意:给你两个字符串一个s,一个t,长度<=2000,要求你进行小于等于6100次的shift操作,将s变成t, shift(x)表示将字符串的最后x个字符翻转后放到 ...
- java实现判断一个经纬度坐标是否在一个多边形内(经自己亲测)
1.在高德地图上绘制的多边形:经纬度逗号分隔格式:上面是用来方便存坐标的对象:下面是方法测试:直接复制代码即可运行 public class Point { private Double x; pri ...
- 6.Django与Ajax
Ajax 文件夹为Ajaxdemo 向服务器发送请求的途径: 1.浏览器地址栏,默认get请求: 2.form表单: get请求 post请求 3.a标签,超链接(get请求) 4.Ajax请求 特点 ...
- 011 处理模型数据时@ModelAttribute的使用
一:说明 1.使用场景 在下面的场景中: 使用new对象时,最后的效果是两个被赋值了,但是还有一个值是空值的,这个不符合只更新两个值,另一个值不变的情况. 正确的做法: 三个值得数据不是new出来的, ...
- 094实战 关于js SDK的程序,java SDK的程序
一:JS SDK 1.修改配置workspace 2.导入 3.Demo.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Trans ...
- copy 深浅复制
- 中间人攻击工具ettercap
中间人攻击工具ettercap (一).简介 (二).模块划分 1.Snifer 2.MITM 3.Filter 4.Log 5.Plugin (三).特性 (四).用户操作界面 (五).指定目标 ( ...