Posted on 2010-06-08 09:43
djx_zh 阅读(2292)
评论(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); // 产生一个线程
}
不知道这种发射多线程的方法会给大家带来方便吗?请各位大侠给点意见。