zoukankan      html  css  js  c++  java
  • boost之thread

    1.boost里的thread创建之后会立即启动。

    代码示例:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/thread.hpp>
    #include <boost/thread/mutex.hpp>
    using namespace std;
    using namespace boost;
    
    
    mutex io_mu;
    void printing(int& x,const string str)
    {
    	for (int i = 0;i < 5;++i)
    	{
    		mutex::scoped_lock lock(io_mu);
    		cout << str << ++x <<endl;
    	}
    }
    
    int main()
    {
    	int x = 0;
    	thread(printing,x,"hello");
    	thread(printing,x,"boost");
    	this_thread::sleep(posix_time::seconds(2));
    	return 0;
    	
    }
    

     2.主线程等待和线程分离,为了防止主线程在子线程未执行完时就退出,可以使用posix_time::seconds和timed_join以及join

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/thread.hpp>
    #include <boost/thread/mutex.hpp>
    using namespace std;
    using namespace boost;
    
    
    mutex io_mu;
    void printing(int& x,const string str)
    {
    	for (int i = 0;i < 5;++i)
    	{
    		mutex::scoped_lock lock(io_mu);
    		cout << str << ++x <<endl;
    	}
    }
    
    int main()
    {
    	int x = 0;
    	thread t1(printing,x,"hello");
    	thread t2(printing,x,"boost");
    	//this_thread::sleep(posix_time::seconds(2));
    	t1.timed_join(posix_time::seconds(1));
    	t2.join();
    	t1.detach();
    	return 0;
    	
    }
    

     3.操作线程,获取线程id和获得可并行(非并发)执行的线程数量。

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/thread.hpp>
    #include <boost/thread/mutex.hpp>
    using namespace std;
    using namespace boost;
    
    
    mutex io_mu;
    void printing(int& x,const string str)
    {
    	for (int i = 0;i < 5;++i)
    	{
    		mutex::scoped_lock lock(io_mu);
    		cout << str << ++x <<endl;
    	}
    }
    
    int main()
    {
    	int x = 0;
    	thread t1(printing,x,"hello");
    	thread t2(printing,x,"boost");
    
    	cout << t1.get_id()<<endl;
    	cout << thread::hardware_concurrency()<<endl;
    	this_thread::sleep(posix_time::seconds(2));
    	
    	return 0;
    	
    }
    

     4.线程中断

    代码示例:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/thread.hpp>
    #include <boost/thread/mutex.hpp>
    using namespace std;
    using namespace boost;
    
    
    mutex io_mu;
    void to_interrupt(int& x,const string str)
    try
    {
    	for (int i = 0;i < 5;++i)
    	{
    		this_thread::sleep(posix_time::seconds(1));
    		mutex::scoped_lock lock(io_mu);
    		cout << str << ++x <<endl;
    	}
    }
    catch(thread_interrupted&)
    {
    	cout << "thread_interrupted" <<endl;
    }
    
    int main()
    {
    	int x = 0;
    	thread t(to_interrupt,x,"hello boost");
    	
    	this_thread::sleep(posix_time::seconds(4));
    	t.interrupt();
    	t.join();
    	
    	return 0;
    	
    }
    

     5.线程组

    线程组可以看做是线程池,内部是使用顺序容器list<thread*>存放tread的指针。

    #include <iostream>
    #include <string>
    #include <vector>
    #include <boost/bind.hpp>
    #include <boost/thread.hpp>
    #include <boost/thread/mutex.hpp>
    using namespace std;
    using namespace boost;
    
    mutex io_mu;
    void printing(int& x,const string str)
    {
    	for (int i = 0;i < 5;++i)
    	{
    		mutex::scoped_lock lock(io_mu);
    		cout << str << ++x <<endl;
    	}
    }
    
    int main()
    {
    	int x = 0;
    	thread_group tg;
    	tg.create_thread(bind(printing,x,"C++"));
    	tg.create_thread(bind(printing,x,"BOOST"));
    	tg.join_all();
    	return 0;
    	
    }
    
  • 相关阅读:
    js判断undefined类型
    【转】 C#操作FTP
    FTP
    Failed to execute request because the App-Domain could not be created. Error: 0x80070002 系统找不到指定的文件。
    [转]C# 安装与部署
    ASP.NET 实现重启系统或关机
    ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
    C#取整函数Math.Round、Math.Ceiling和Math.Floor
    Oracle 更改字符集 更改后之前的中文全成乱码了
    oracle
  • 原文地址:https://www.cnblogs.com/liuweilinlin/p/3256206.html
Copyright © 2011-2022 走看看