手机版
你好,游客 登录 注册
背景:
阅读新闻

C++实现线程池的经典模型

[日期:2013-02-25] 来源:Linux社区  作者:rao_warrior [字体:大 中 小]

void * test(void* i){         
    printf("test:%d\n",( int)i);         
    sleep(1);         
    return NULL; 
} 
int main(int argc,char ** args){         
    printf("this is main!\n");         
    class pmulti pm;         
    pm.init(15);         
    worker_node no ;         
    for(int i=0;i<5;i++){                 
        no.process = &test ;                 
        no.arg = (void*)(i*2);                 
        pm.add_worker(&no);         
    }         
    sleep(5);         
    pm.destroy();      
    return 0; 
} 

运行结果:
g++ multi.cpp multi.h -lpthread -o multi
[test@localhost multi_thread]$ ./multi
this is main!
thread num:24531909488
test:0
thread num:3046583152
test:2
thread num:3036093296
test:4
thread num:3025603440
test:6
thread num:3015113584
test:8
thread num:3004623728
thread num:3067562864
thread num:2994133872
thread num:2983644016
thread num:2973154160
thread num:2962664304
thread num:2952174448
thread num:2941684592
thread num:2931194736
thread num:3078052720
thread_fun:24531909488 run
thread_fun:3046583152 run
thread_fun:3036093296 run
thread_fun:3025603440 run
thread_fun:3015113584 run
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;
joined :0  end;

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款