string

string
posts - 27, comments - 177, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

thread wrapper

Posted on 2010-06-08 09:43 djx_zh 阅读(2281) 评论(6)  编辑 收藏 引用

 最近做了一套thread wrapper, 可以以线程的形式运行任意函数, 例子如下:

#ifdef  _PTHREAD
#include <pthread.h>
void* thread_func_g(void*p){
 funcInfo_t* ft = (funcInfo_t*)p;
 __asm__(
 "subl %%ecx, %%esp\n"
 "movl %%esp, %%edi\n"
 "rep movsb\n"
 "call *%%eax\n"
 :
 :"a"(ft->f),"c"(ft->argSize), "S"(ft->esp)
 :"%edi");
}
#else
#endif

typedef struct{
 void* f;
 int argSize;
 char esp[128];
}funcInfo_t;

funcInfo_t global_funcInfo;
#define launchTemplateThread() \
create_thread(thread_func_g, &global_funcInfo);// /*thread_func_g(&global_funcInfo);*/

///////////////////// 3 args ////////////////////////////////////////////
#define slaunchArg3( a00, a01, a02) {char* pcur= global_funcInfo.esp; \
 (*(__typeof__(a00)*)pcur)=a00; pcur += _INTSIZEOF(__typeof__(a00));\
 (*(__typeof__(a01)*)pcur)=a01; pcur += _INTSIZEOF(__typeof__(a01));\
 (*(__typeof__(a02)*)pcur)=a02; pcur += _INTSIZEOF(__typeof__(a02));\
 global_funcInfo.argSize = pcur - global_funcInfo.esp;\
}\
launchTemplateThread();

#define slaunch3(sfunc) global_funcInfo.f = (void*) sfunc; slaunchArg3

#define kernel_ret  void* __attribute__((stdcall))

kernel_ret foo(int a, int b, int c){
printf("%d %d %d\n", a, b,c);
return 0;


int main(){
int a, b, c;
char cc;
slaunch3(foo)(a, b, c);        // 产生一个线程
slaunch3(foo)(a, b, (int)c); // 产生一个线程
}

不知道这种发射多线程的方法会给大家带来方便吗?请各位大侠给点意见。

Feedback

# re: thread wrapper  回复  更多评论   

2010-06-08 12:19 by 天堂的隔壁
有一些简单问题复杂化
跨平台问题平台绑定的意思
而且还有多线程问题

# re: thread wrapper  回复  更多评论   

2010-06-08 13:59 by djx_zh
@天堂的隔壁
多谢赐教。 是搞的挺复杂的。但可以把这套复杂的东西放到头文件里,用户不用关心。 用户只要调用slaunchX就可以了, 不过这种简单的线程好像不大实用。

# re: thread wrapper  回复  更多评论   

2010-06-08 15:28 by 陈梓瀚(vczh)
@djx_zh

int 囧(int a, string b, void* c)
{
throw "囧";
}

class 囧Getter
{
public:
void Get囧(int result);
}

--------------------
囧Getter 囧getter;
RunAsync(囧, Func<void(int)>(&囧getter, &囧Getter::Get囧), 1, "vczh", 0);
RunAsync(囧, 1, "vczh", 0);
--------------------
这样就好用了,因为C++编译器会替你检查错误。譬如说穿进去的囧函数第二个参数是float,编译器就会抱怨你参数"vczh"不对
--------------------
一般来说你只需要支持0到10个参数,还有返回void或者有返回值的函数,特化22个就行了。

# re: thread wrapper  回复  更多评论   

2010-06-08 15:35 by djx_zh
@陈梓瀚(vczh)
对,模板比宏好用。

# re: thread wrapper  回复  更多评论   

2010-06-09 17:57 by 梅发坤
呵呵,用到了汇编,看的头大,我模板做了一个如何


template< typename R, typename T>
R _cast(T t)
{
return t;
}

template< typename R>
R _cast(std::tr1::shared_ptr< typename std::tr1::remove_pointer<R>::type > t)
{
return t.get();
}

template<typename F>
class thread_closure_0
{
public:

thread_closure_0(const F& f, const tchar* nm = 0)
: _function(f)
, _name((0 == nm) ? _T("") : nm)
{
}

static void start_routine(void* c)
{
thread_closure_0 *self = static_cast<thread_closure_0*>(c);
try
{
self->_function();
}
catch (...)
{
delete self;
throw;
}

delete self;
}

const tstring& name() const
{
return _name;
}
private:
F _function;
tstring _name;
};

template<typename F, typename P>
class thread_closure_1
{
public:

thread_closure_1(const F& f, const P& x, const tchar* nm = 0)
: _function(f)
, _name((0 == nm) ? _T("") : nm)
, arg1(x)
{
}

static void start_routine(void* c)
{
thread_closure_1 *self = static_cast<thread_closure_1*>(c);
try
{
self->_function(self->arg1);
}
catch (...)
{
delete self;
throw;
}
delete self;
}

const tstring& name() const
{
return _name;
}
private:
F _function;
tstring _name;
P arg1;
};

template<typename F>
inline void create_thread(const F& f, const tchar* nm = null_ptr)
{
typedef thread_closure_0<F> closure_type;

// _beginthread创建线程时,其返回值可能是一个无效句柄(太快而被关闭了),千万不要对它作操
// 作,如 WaitForSingleObject 等,具体见帮助
uintptr_t handle = ::_beginthread(closure_type::start_routine, 0, new closure_type(f, nm));
if (-1L != handle)
return;

if (null_ptr != nm)
ThrowException1(RuntimeException, _T("启动线程[") + tstring(nm) + _T("]失败"));
else
ThrowException1(RuntimeException, _T("启动线程失败"));
}

template<typename F, typename P>
inline void create_thread(const F& f, const P& x, const tchar* nm = null_ptr)
{
typedef thread_closure_1<F, P> closure_type;

// _beginthread创建线程时,其返回值可能是一个无效句柄(太快而被关闭了),千万不要对它作操
// 作,如 WaitForSingleObject 等,具体见帮助
uintptr_t handle = ::_beginthread(closure_type::start_routine, 0, new closure_type(f, x, nm));
if (-1L != handle)
return;

if (null_ptr != nm)
ThrowException1(RuntimeException, _T("启动线程[") + tstring(nm) + _T("]失败"));
else
ThrowException1(RuntimeException, _T("启动线程失败"));
}

# re: thread wrapper  回复  更多评论   

2010-06-09 19:08 by djx_zh
@梅发坤
Cool, 这样就清晰多了,还容易调试。

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理