BOOST多线程

来源:互联网 发布:只有我知见面会完整版 编辑:程序博客网 时间:2024/06/06 07:03

1,使用全局函数作为线程执行体


void Func(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        cout <<  __FUNCTION__ << i << endl;
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread th(Func, 100);
    //等待线程结束
    th.join();
}



2,使用成员函数作为线程执行体

class A
{
public:
    void Func(int nCount)
    {
        for (int i = 0; i < nCount; i++)
        {
            cout <<  __FUNCTION__ << i << endl;
        }
    }
};
//线程参数都采用值传递,因此即使如下传入一个临时变量作为参数,线程照样可以正确运行
//如果需要传递引用,可通过ref库来实现
boost::thread *pth;
void TestThread()
{
    A a;
    //线程绑定一个局部变量
    pth = new boost::thread( &A::Func, &a, 100);
}

C,仿函数作为线程执行体

class  B
{
public:
    B(int n):nMem(n)
    {
    }
    void operator()()
    {
        for (int i = 0; i < nMem; i++)
        {
            cout <<  __FUNCTION__ << i << endl;
        }
    }
    int nMem;
};
//线程thread对象销毁时,会与线程执行体分离,线程执行体不受影响
void TestThread2()
{
    //创建临时线程对象
    boost::thread(B(100));
}

4,中断线程

成员函数interrupt。运行正在执行的线程中断,被中断的线程会抛出异常类boost::thread_interrupted,程序应该自行处理该异常


void interrupt_thread(int nCount)
{
    try
    {
        for (int i = 0; i < nCount; i++)
        {
            //sleep函数允许中断
            boost::this_thread::sleep(boost::posix_time::seconds(1));
            cout <<  __FUNCTION__ << i << endl;
        }
    }
    catch(boost::thread_interrupted&)
    {
        cout << "thread interrupt" << endl;
    }
}
boost::thread th2(interrupt_thread, 100);
boost::this_thread::sleep(boost::posix_time::seconds(4));
th2.interrupt();


============================================================》》》》》》》》》》

=============================================================================》》》》》》》》》》》》》》》》》》》》》》》

========================================================================================》》》》》》》》》》》》》》》》》》》》》》》》》》》》

boost库多线程间通信

1,互斥锁

在编程中,引入了对象互斥锁的概念,来保证共享数据操作的完整性。每个对象都对应于一个可称为“互斥锁”的标记。

这个标记用来保证在任一时刻,只能有一个线程访问该对象


#include <iostream>
#include <boost/thread.hpp>
using namespace std;
int g_num = 0;
boost::mutex mu;  //定义互斥锁对象
int Func(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        boost::mutex::scoped_lock lock(mu);  //对共享数据进行操作,需加锁
        g_num++;
        cout << __FUNCTION__ << ": " << g_num << endl;
    }
    return g_num;
}
int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread th1(Func, 100);
    boost::thread th2(Func, 200);
    th1.join();
    th2.join();
    return 0;
}

2,读写锁

boost::shared_mutex rw_mu;   //定义读写锁
int Write(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        boost::unique_lock<boost::shared_mutex> lock(rw_mu);   //加唯一锁
        g_num++;
        cout << __FUNCTION__ << ": " << g_num << endl;
    }
    return g_num;
}
void Read(int nCount)
{
    for (int i = 0; i < nCount; i++)
    {
        boost::shared_lock<boost::shared_mutex> lock(rw_mu);  //加共享锁
        cout << __FUNCTION__ << ": " << g_num << endl;
    }
}
int _tmain(int argc, _TCHAR* argv[])
{
    boost::thread th1(Write, 100);
    boost::thread th2(Read, 100);
    boost::thread th3(Read, 100);
    th1.join();
    th2.join();
    th3.join();
    return 0;
}



3,条件量

条件量相对于互斥锁和读写锁来说,并不是那么好理解,简单来说,


原创粉丝点击