Posted on 2010-06-08 09:43
djx_zh 閱讀(2312)
評(píng)論(6) 編輯 收藏 引用
最近做了一套thread wrapper, 可以以線程的形式運(yùn)行任意函數(shù), 例子如下:
#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); // 產(chǎn)生一個(gè)線程
slaunch3(foo)(a, b, (int)c); // 產(chǎn)生一個(gè)線程
}
不知道這種發(fā)射多線程的方法會(huì)給大家?guī)?lái)方便嗎?請(qǐng)各位大俠給點(diǎn)意見(jiàn)。