Mfc的多线程看起来简单,可以把线程直接压入向量,由系统类似进行调配,其实在内存的处理问题上留下了漏洞。在新线程里面载入大量流,会导致内存泄露。

方便之处:直接使用结构体传入函数参数,供函数使用。

使用boost多线程,boost库给出了一个相对理想的多线程使用组合库。

      参考链接:http://blog.csdn.net/iamnieo/article/details/2908621

一:使用参数的boost进程:

(1):建立参数类

class  BoostThreadFunc       {

public:

    pcl::PointCloud<pcl::PointXYZRGB>  ModelCloud;
MyRect BBX;
CAviTestDlg* dlg; //使用主框的变量 public: BoostThreadFunc();//构造函数.... BoostThreadFunc( pcl::PointCloud<pcl::PointXYZRGB> &ModelCloud , MyRect &BBX , CAviTestDlg* dlg ) { this->BBX = BBX;
this->ModelCloud =ModelCloud;
this->dlg = dlg; }//重载构造函数.... void run( pcl::PointCloud<pcl::PointXYZRGB> &ModelCloud,
MyRect &BBX,
CAviTestDlg* &dlg );//主要运行函数.... //创建函数对象....
void operator()() { this->run( this->ModelCloud, this->RatioRange, this->BBX, this->dlg); } }; void BoostThreadFunc ::run( pcl::PointCloud<pcl::PointXYZRGB> &ModelCloud,
MyRect &BBX,
CAviTestDlg* &dlg )
{
.............................................................
}//主要运行函数....

(2):创建一个线程:创建线程

boost::thread myThread(threadFun);

需要注意的是:参数可以是函数对象或者函数指针。并且这个函数无参数,并返回void类型

所以,取巧的方式是把参数放入 构造函数里面,使用函数对象调用运行函数。

使用方式:

BoostThreadFunc      MyThreadFunc( *ModelCloud,  BBX,   this);//在主框里调用boost线程.
boost::thread MyThread( MyThreadFunc);
MyThread.join();

注意:使用的过程中脑残了一番:boost::thread  MyThread(& MyThreadFunc); 造成thread.hpp的f()函数无法展开!什么原因???

是否?boost::thread  MyThread( MyThreadFunc);载入的是无参数函数对象/函数指针,而boost::thread  MyThread(& MyThreadFunc);中若使用&必须绑定全局静态函数.

二:参考链接:http://www.cnblogs.com/younes/archive/2010/06/06/1752745.html

使用线程组

如果你需要创建几个线程,考虑使用一个线程组对象thread_group来组织它们。一个thread_group对象可以使用多种方法管理线程。首先,可以使用一个指向动态创建的线程对象的指针作为参数来调用add_thread方法,将这个线程加入线程组。也可以直接使用线程组类的create_thread方法,可不先创建线程而直接把线程加入到线程组中。

当线程组对象的析构函数被调用时,它将删除(delete)所有这些通过add_thread方法加入的线程指针。所以,只能将堆上的线程对象指针通过add_thread方法加入线程组。remove_thread方法从线程组删除某个线程的指针,但是我们仍需负责把线程本身内存释放掉。

线程组对象的成员方法join_all方法等待线程组中所有线程结束,才返回。

boost::thread_group                                 grp;
boost::thread *p =newboost::thread(threadFun);
grp.add_thread(p);
//do something...
grp.remove_thread(p); grp.create_thread(threadFun);
grp.create_thread(threadFun); //Now there are two threads in grp grp.join_all(); //Wait for all threads to finish

三、使资源是线程安全的


    保证同一时刻多个线程不会同时修改同一个共享资源,那么这个程序是线程安全的,或者是串行化访问资源的。可以使用mutex类来控制线程的并发问题。

0
#include <iostream>
#include <boost/thread/thread.hpp>
#include <string>
 
// A simple queue class; don't do this, use std::queue
template<typenameT>
classQueue {

public:
   Queue( ) {}
  ~Queue( ) {}
 
   voidenqueue(constT& x) {
     

      boost::mutex::scoped_lock lock(mutex_);// Lock the mutex for this queue
      list_.push_back(x);// A scoped_lock is automatically destroyed (and thus unlocked)//
when it goes out of scope
   }
 
   T dequeue( ) {
      boost::mutex::scoped_lock lock(mutex_);
 
      if(list_.empty( ))
         throw"empty!";     // This leaves the current
scope, so the
      T tmp = list_.front( );// lock is released
      list_.pop_front( );
      return(tmp);
   }// Again: when scope ends, mutex_ is unlocked
 
private:
   std::list<T> list_;
   boost::mutex mutex_;

};
 

Queue<std::string> queueOfStrings;
 
void sendSomething( ) {
   std::string s;
   for(inti = 0; i < 10; ++i) {
      queueOfStrings.enqueue("Cyrus");
   }
}
 
void recvSomething( ) {
   std::string s;
 
   for(inti = 0; i < 10; ++i) {
      try{s = queueOfStrings.dequeue( );}
      catch(...) {}
   }
}
 
intmain( ) {

   boost::threadthr1(sendSomething);
   boost::threadthr2(recvSomething);
 
   thr1.join( );
   thr2.join( );
}

mutex对象本身并不知道它代表什么,它仅仅是被多个消费者线程使用的资源访问的锁定解锁标志。在某个时刻,只有一个线程可以锁定这个mutex对象,这就阻止了同一时刻有多个线程并发访问共享资源。一个mutex就是一个简单的信号机制。

给mutex加解锁有多种策略,最简单的是使用scoped_lock类,它使用一个mutex参数来构造,并一直锁定这个mutex直到对象被销毁。如果这个正在被构造的mutex已经被别的线程锁定的话,当前线程就会进入wait状态,直到这个锁被解开。

三、读写锁


mutex有一个美中不足,它不区分读和写。线程如果只是进行读操作,mutex强制线程串行化访问资源,效率低。而且这种操作不需要排他性访问。基于这个原因,Boost线程库提供了read_write_mutex。

0
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/read_write_mutex.hpp>
#include <string>
 
template<typenameT>
classQueue {

public:
   Queue( ) :
   rwMutex_(boost::read_write_scheduling_policy::writer_priority){}

   // Use a read/write mutex and give writers priorit
  ~Queue( ) {}

 
   voidenqueue(constT& x) {
     
// Use a r/w lock since enqueue updates the state

      boost::read_write_mutex::scoped_write_lock writeLock(rwMutex_);
      list_.push_back(x);
   }
 
   T dequeue( ) {
      // Again, use a write lock

      boost::read_write_mutex::scoped_write_lock writeLock(rwMutex_);
 
      if(list_.empty( ))
         throw"empty!";
      T tmp = list_.front( );
      list_.pop_front( );
      return(tmp);
   }
 
   T getFront( ) {
      // This is a read-only operation, so you only need a read lock

      boost::read_write_mutex::scoped_read_lock readLock(rwMutex_);
      if(list_.empty( ))
         throw"empty!";
      return(list_.front( ));
   }
 
private:
   std::list<T> list_;
   boost::read_write_mutex rwMutex_;

};
 
Queue<std::string> queueOfStrings;
 
void  sendSomething( ) {
   std::string s;
 
   for(inti = 0; i < 10; ++i) {
      queueOfStrings.enqueue("Cyrus");
   }
}
 
void  checkTheFront( ) {
   std::string s;
 
   for(inti = 0; i < 10; ++i) {
      try{s = queueOfStrings.getFront( );}
      catch(...) {}
   }
}
 
int main( ) {
 
   boost::threadthr1(sendSomething);
   boost::thread_group grp;
 
   grp.create_thread(checkTheFront);
   grp.create_thread(checkTheFront);
   grp.create_thread(checkTheFront);
   grp.create_thread(checkTheFront);
 
   thr1.join( );
   grp.join_all( );

}

注意Queue的构造函数中队读写锁rwMutex的初始化。同一时刻,可能有多个读写线程要锁定一个read_write_mutex,而这些锁的调度策略依赖于构造这个mutex时选定的调度策略。Boost库中提供了四种调度策略:

1)reader_priority:等待读锁的线程优先于等待写锁的线程

2)writer_priority:等待写锁的线程优先于等待读锁的线程

3)alternating_single_read:在读锁和写锁之间交替

4)alternating_many_reads:在读锁和写锁之间交替,这个策略将在两个写锁之间使得所有的在这个queue上挂起的读锁都被允许。

选择使用哪种策略要慎重,因为使用前两种的话可能会导致某些锁始终不能成功,出现饿死的现象。

死锁、饿死和竞态条件

1)死锁,是涉及至少2个线程和2个资源的情况。线程A和B,资源X和Y。A锁定了X,而B锁定了Y。此时A和B有彼此想要对方的资源,死锁就出现了。

死锁的预防有两种方法。一种是,通过小心的按照一定的顺序对不同的mutex来加锁。另一种是,使用Boost提供的try_mutex互斥量和scoped_try_lock。或者使用时间锁。scoped_try_lock对try_mutex加锁时,可能成功,也可能失败,但不会阻塞。时间锁则有一个超时时间。

0
booldequeue(T& x)
{
    boost::try_mutex::scope_try_lock lock(tryMutex_);
    if(!lock.locked())
        returnfalse;
    else{
        if(list_.empty())
            throw"empty!";
        x = list_.front();
        list_.pop_front();
        returntrue;
    }
}
private:
    boost::try_mutex tryMutex_;

2)饿死,如果你正在使用write_priority策略,并且你有很多创建写锁的线程,那么读锁的线程就可能饿死。

3)竞态条件,

0
if(q.getFront() =="Cyrus"){
   str = q.dequeue();
   //....
}

这个代码在单线程环境中工作很好,因为q在第一行和第二行代码之间不会被修改。多线程环境中则会出现问题。此为竞态条件。解决的方法是为Queue添加一个成员函数dequeueIfEquals,在函数执行过程中始终锁定互斥量。

四、从一个线程中给另一个线程发送通知


当需要线程等待某个事物时,可以创建一个condition对象,然后通过这个对象来通知那些等待的线程。

0
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <list>
#include <string>
 
classRequest {/*...*/};
 
// A simple job queue class; don't do this, use std::queue

template<typenameT>
classJobQueue {

public:
   JobQueue( ) {}
  ~JobQueue( ) {}
 
   void submitJob(constT& x) {

      boost::mutex::scoped_lock lock(mutex_);
      list_.push_back(x);
      workToBeDone_.notify_one( );
   }
 
   T getJob( ) {
      boost::mutex::scoped_lock lock(mutex_);
 
      workToBeDone_.wait(lock);// Wait until this condition is
                              // satisfied, then lock the mutex
      T tmp = list_.front( );
      list_.pop_front( );
      return(tmp);
   }
 
private:
   std::list<T> list_;
   boost::mutex mutex_;
   boost::condition workToBeDone_;
};
 
JobQueue<Request> myJobQueue;
 
void boss( ) {
   for(;;) {
      // Get the request from somewhere
      Request req;
      myJobQueue.submitJob(req);
   }
}
 
void worker( ) {
   for(;;) {
      Request r(myJobQueue.getJob( ));
      // Do something with the job...
   }
}
 
int main( ) {
   boost::threadthr1(boss);
   boost::threadthr2(worker);
   boost::threadthr3(worker);
 
   thr1.join( );
   thr2.join( );
   thr3.join( );
}

boost::mutex::scoped_lock lock(mutex_);

workToBeDone_.wait(lock);

这两行代码,第一行锁定这个mutex对象。第二行代码解开这个mutex上的锁,然后进行等待或者休眠,直到它的条件得到了满足。这个mutex互斥对象的解锁让其他的线程能够使用这个mutex对象,它们中的某个需要设置这个等待条件,之后通知另外的线程。

notify_all函数,通知那些所有正在等待某个条件变为真的线程,那些线程随后进入运行状态。wait方法做两件事情:它一直等待直到有人在它正等待的condition上调用notify_one或notify_all,然后它就试图锁定相关的mutex。当调用的是notify_all时,尽管多个等待的线程都尽量去获得下一个锁,但谁将获得依赖于这个mutex的类型和使用的优先策略。

一个condition对象能让消费者线程休眠,因此在还没有碰到一个condition时处理器可以去处理别的事情。例如一个web服务器使用一个工作线程池来处理进来的请求。当没有需求进来时,让这些子线程处于等待状态比让它们循环的查询或者睡眠然后偶尔唤醒来检查这个队列,要好很多。

五、只初始化一次共享资源


0
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/once.hpp>
 
// Some sort of connection class that should only be initialized once
structConn {

   static void init( ) { ++i_; }
   staticboost::once_flag init_;
   staticint i_;
   // ...
};
 
int Conn::i_ = 0;
boost::once_flag Conn::init_ = BOOST_ONCE_INIT;
 
void worker( ) {
   boost::call_once(Conn::init, Conn::init_);
   // Do the real work...
}
 
Conn c; 
// You probably don't want to use a global, so see the
         // next Recipe
 
intmain( ) {
 
   boost::thread_group grp;
 
   for(inti = 0; i < 100; ++i)
      grp.create_thread(worker);
 
   grp.join_all( );
 
   std::cout << c.i_ <<'\n';// c.i_ = 1
}

一个共享资源不得不在某个地方被初始化,并且你希望第一次使用这个资源的线程来完成初始化工作。一个once_flag类型和call_once函数能够保证多个线程不会重复的初始化同一个对象。首先,必须使用BOOST_ONCE_INIT宏来初始化这个once_flag对象。boost::once_flag Conn::init_ = BOOST_ONCE_INIT; 之后调用call_once函数,boost::call_once(Conn::init, Conn::init_); 第一个形参是希望被执行一次的初始化函数的地址。

六、给线程函数传递一个参数


0
#include <iostream>
#include <string>
#include <functional>
#include <boost/thread/thread.hpp>
 
// A typedef to make the declarations below easier to read
typedefvoid(*WorkerFunPtr)(conststd::string&);
 
template<typenameFunT,  // The type of the function
being called
         typenameParamT >// The type of its parameter

structAdapter {

   Adapter( FunT f, ParamT& p) :// Construct this adapter and set the
      f_(f), p_(&p) {}         // members to the function and its arg
 
   void operator( )( ) {// This just calls the function with its arg
      f_(*p_);        
   }
private:
   FunT    f_;
   ParamT* p_; // Use the parameter's address to avoid extra copying
};
 
void worker(const std::string& s) {
   std::cout << s <<'\n';
}
 
intmain( ) {
 
   std::string s1 ="This is the first thread!";
   std::string s2 ="This is the second thread!";
 
   boost::threadthr1(Adapter<WorkerFunPtr, std::string>(worker, s1));
   boost::threadthr2(Adapter<WorkerFunPtr, std::string>(worker, s2));
 
   thr1.join( );
   thr2.join( );
}

使用这个函数适配器类模板,你就可以给线程函数传递参数了。如果你需要传递多个参数,仅需要在这个适配器中增加另一个类型和成员变量。

Boost多线程-替换MFC线程的更多相关文章

  1. MFC 线程创建方式

    MFC 分UI线程和工作线程,一般现在的应用程序都是一个主UI线程和N个工作线程来完成工作.主UI线程获取到工作线程发送的信息来刷新界面. 不过因为工作需要,MFC有要维护的项目,因此就学习一下MFC ...

  2. VC中利用多线程技术实现线程之间的通信

    当前流行的Windows操作系统能同时运行几个程序(独立运行的程序又称之为进程),对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程,线程提供了多任务处理的能力.用进程和线程的观点来研究软 ...

  3. Boost多线程编程

    Boost多线程编程   背景 •       今天互联网应用服务程序普遍使用多线程来提高与多客户链接时的效率:为了达到最大的吞吐量,事务服务器在单独的线程上运行服务程序: GUI应用程序将那些费时, ...

  4. 【C/C++学院】0904-boost智能指针/boost多线程锁定/哈希库/正則表達式

    boost_array_bind_fun_ref Array.cpp #include<boost/array.hpp> #include <iostream> #includ ...

  5. Python中的多线程编程,线程安全与锁(二)

    在我的上篇博文Python中的多线程编程,线程安全与锁(一)中,我们熟悉了多线程编程与线程安全相关重要概念, Threading.Lock实现互斥锁的简单示例,两种死锁(迭代死锁和互相等待死锁)情况及 ...

  6. Boost多线程

    一.概述     线程是在同一程序同一时间内允许执行不同函数的离散处理队列,这使得在一个长时间进行某种特殊运算的函数在执行时不阻碍其他的函数时变得十分重要.线程实际上允许同时执行两种函数,而这两者不必 ...

  7. boost多线程使用简例

    原文链接:http://www.cppblog.com/toMyself/archive/2010/09/22/127347.html C++ Boost Thread 编程指南 转自cnblog: ...

  8. Java多线程系列--“JUC线程池”06之 Callable和Future

    概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...

  9. .NET面试题解析(07)-多线程编程与线程同步

      系列文章目录地址: .NET面试题解析(00)-开篇来谈谈面试 & 系列文章索引 关于线程的知识点其实是很多的,比如多线程编程.线程上下文.异步编程.线程同步构造.GUI的跨线程访问等等, ...

随机推荐

  1. autoware安装

    1.Autoware的地址为https://github.com/CPFL/Autoware2.Install dependencies for Ubuntu 16.04 kinetic安装教程ins ...

  2. layui 导出excel复杂表头

    众所周知 layui的导出功能很好用,但是今天我要给大家推荐一个更好用的 大家来到这里想必也是因为layui无法满足 [导出Excle复杂表头] 的业务需求而来,这里废话不多说但还是强调一点,如果你是 ...

  3. jQuery(UI)常用插件

    jQuery 官方网站:http://jquery.com/ 下载地址:http://jquery.com/download/ 插件地址: http://plugins.jquery.com/ 常用插 ...

  4. BZOJ 1500 Luogu P2042 [NOI2005] 维护数列 (Splay)

    手动博客搬家: 本文发表于20180825 00:34:49, 原地址https://blog.csdn.net/suncongbo/article/details/82027387 题目链接: (l ...

  5. Mysql学习总结(12)——21分钟Mysql入门教程

    21分钟 MySQL 入门教程 目录 一.MySQL的相关概念介绍 二.Windows下MySQL的配置 配置步骤 MySQL服务的启动.停止与卸载 三.MySQL脚本的基本组成 四.MySQL中的数 ...

  6. 对Django框架中Cookie的简单理解

    概念的理解:首先Cookie和Session一样,是django中用于视图保持状态的方案之一.为什么要进行视图保留呢,这是因为浏览器在向服务器发出请求时,服务器不会像人一样,有记忆,服务器像鱼一样,在 ...

  7. java构造函数重载this(true)

    看storm的代码的时候,发现这样一句java代码, 很是不理解 google之后,发现原来是java语法中,构造函数重载,this()调用的其实就是 构造函数.This is constructor ...

  8. asp.net控件的异步刷新

    需求:我们知道,asp.net控件中的button控件,默认是开启了自己主动回发的,而有时候.我们不想刷新整个界面.而仅仅想局部刷新,可页面中又偏偏用到了.net button控件. 尽管我非常讨厌. ...

  9. 问题2-:Syntax error on tokens, delete these tokens

    出现原因:拷贝下来的代码缺少{左大括号 然后运行时run as 没有选到java application 是因为没有main方法 加个public static void main(String() ...

  10. java 线程死锁的检测

    java 线程死锁的检测   例子程序: import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executo ...