当前位置: 首页 > news >正文

电商服务站点建设方案网站数据库安装教程

电商服务站点建设方案,网站数据库安装教程,大量增加告权重网站友链回提升网站权重吗,建设机械网站咨询引言#xff1a; 此次我们重新封装线程#xff0c; 采用基于对象编程的方式#xff0c;不用于面向对象编程中重定义虚函数的方式#xff0c;这里我们用回调函数的方式。 Thread.h 1 #ifndef THREAD_H_2 #define THREAD_H_3 4 #include boost/noncopyable.hpp5 #inc…引言 此次我们重新封装线程 采用基于对象编程的方式不用于面向对象编程中重定义虚函数的方式这里我们用回调函数的方式。 Thread.h 1 #ifndef THREAD_H_2 #define THREAD_H_3 4 #include boost/noncopyable.hpp5 #include functional6 #include pthread.h7 8 class Thread : boost::noncopyable9 { 10 public: 11 typedef std::functionvoid () ThreadCallback; 12 13 Thread(ThreadCallback callback); 14 ~Thread(); 15 16 void start(); 17 void join(); 18 19 static void *runInThread(void *); 20 21 private: 22 pthread_t threadId_; 23 bool isRunning_; 24 ThreadCallback callback_; //回调函数 25 }; 26 27 28 29 #endif //THREAD_H_   可以看到没有重定义虚函数 而是设置了一个函数适配器 用于保存我们想要的业务逻辑。 直接用 静态函数 runInThread 调用 callback_即可。   Thread.cpp 1 #include Thread.h2 3 Thread::Thread(ThreadCallback callback)4 : threadId_(0),5 isRunning_(false),6 callback_(std::move(callback))7 {8 9 } 10 11 Thread::~Thread() 12 { 13 if(isRunning_) 14 { 15 //detach 16 pthread_detach(threadId_); 17 } 18 } 19 20 void Thread::start() 21 { 22 pthread_create(threadId_, NULL, runInThread, this); 23 isRunning_ true; 24 } 25 void Thread::join() 26 { 27 pthread_join(threadId_, NULL); 28 isRunning_ false; 29 } 30 31 void *Thread::runInThread(void *arg) 32 { 33 Thread *pt static_castThread*(arg); 34 pt-callback_(); //调用回调函数 35 36 return NULL; 37 } 以上 有几点经验处理 Google推荐 当Thread 这个类析构时如果线程还没有执行完 那么就detach。 并设置一个标志位 bool isRunning 标志线程是否启动。   测试代码 1 #include Thread.h2 #include stdio.h3 #include unistd.h4 using namespace std;5 6 void foo()7 {8 while(1)9 { 10 printf(foo\n); 11 sleep(1); 12 } 13 } 14 15 16 17 int main(int argc, char const *argv[]) 18 { 19 Thread t(foo); 20 21 t.start(); 22 t.join(); 23 24 return 0; 25 } 可以看到 当用基于对象编程时 不需要在定义用户自己的类了 只需在主函数传入一个函数适配器即可。 具体函数类型可以用bind来实现。   测试代码2 1 #include Thread.h2 #include stdio.h3 #include unistd.h4 using namespace std;5 6 class Foo7 {8 public:9 void foo(int i) 10 { 11 while(1) 12 { 13 printf(foo %d\n, i); 14 sleep(1); 15 } 16 } 17 }; 18 19 20 21 int main(int argc, char const *argv[]) 22 { 23 Foo f; 24 int i 34; 25 Thread t(bind(Foo::foo, f, i)); 26 27 t.start(); 28 t.join(); 29 30 return 0; 31 }   对于 类的成员函数 同理使用 大杀器bind  转载于:https://www.cnblogs.com/DLzhang/p/4023369.html
http://www.zqtcl.cn/news/610280/

相关文章:

  • 企业网站改版升级成都便宜网站建设公司
  • 广州公共资源建设工程交易中心网站新塘做网站
  • 数码港 太原网站开发公司iis 建立子网站
  • 做一个自己的网站需要什么商标设计网站猪八戒
  • 傻瓜式网站建设软件保险预约
  • 网站 备案规定自己做简单网站
  • 网站上怎么做支付接口南乐网站建设
  • 咸阳网站建设公司电话做个公司网站大概多少钱
  • 网站如何做关键词排名点子网创意网
  • 浙江建设培训考试网站河源东莞网站建设
  • 网站移动端做pc端的301跳转哪些网站是增值网
  • wordpress新闻站浙江耀华建设集团网站
  • 网站开发代理企业网站推广技巧和方法
  • 俄语网站开发用模板做的网站多少钱
  • 丽水网站建设公司广州网络公司
  • 做基金的网站哪个好针对大学生推广引流
  • 国外对旅游网站的建设互联网推广和互联网营销
  • 海南省建设厅网站首页有什么做设计的兼职网站
  • 网站导航功能苏州市高新区建设局网站
  • jsp网站 值班多语种网站开发
  • 公司网站英文做电商
  • 合肥企业网站建设公司哪家好卖产品怎么做网站
  • 网站建设公司86215中国中小企业网站
  • 做网站 如何 挣钱游戏网站开发协议
  • 网站建设发展wordpress比较慢
  • 收费网站推广动漫制作就业方向
  • 湖北优化网站建设设计公司需要什么资质
  • 个人网站怎么制作wordpress创意小工具
  • 网站管理维护怎么做在线oa
  • vue做企业网站wordpress 不发送邮件