boost使用(二)

今天有时间了,于是决定写点boost使用的东西,这里主要讲下其中thread库和signal库的使用,代码采用最简单的方式编写,不带任何其他的杂质,旨在教会大家两个库的简单使用。好了贴代码:

#include <boost/thread.hpp>
#include <boost/signal.hpp> 
#include <boost/bind.hpp> 
#include <iostream> 
#include <memory> 
#include <iostream> 




class world :
	public boost::signals::trackable
{
public:
	static void a() {}
	void hello(int a) const
	{
		std::cout << a << std::endl;
		std::cout << "Hello, world!" << std::endl;
	}
};

void wait(int seconds)
{
	boost::this_thread::sleep(boost::posix_time::seconds(seconds));
}

void thread()
{
	for (int i = 0; i < 5; ++i)
	{
		wait(1);
		std::cout << i << std::endl;
	}
}

class HelloWorld
{
public:
	void hello(const std::string& str)
	{
		std::cout << str << std::endl;
	}
};


int main()
{
	boost::thread t(thread);
	t.join();


	boost::signal<void(int) > s;
	{
		std::auto_ptr<world> w(new world());
		std::auto_ptr<world> jjf(new world());
		s.connect(boost::bind(&world::hello, w.get(),_1));
		w->a();
		
		std::cout << "in { } " << s.num_slots() << std::endl;
		s(3);
		
	}
	std::cout << "out { } " << s.num_slots() << std::endl;
	s(4);

	HelloWorld obj;
	boost::thread thrd(boost::bind(&HelloWorld::hello, &obj, "Hello World, I'm a thread!"));
	thrd.join();

	system("pause");

	return 0;
}

免得被人说无图无真相,然后附上程序截图:



好了进行简单的说明:bind这个上一节已经说过了,这里就不进行讲解了,然后让我们快速查看一下信号槽,这个就是一个简单的信号量的声明,说明这个signal的处理函数是一个void类型的,接收一个为int类型的参数,我们在{}中声明两个智能指针的变量体,然后对信号槽进行处理函数的绑定,绑定之后,我们就立刻触发下这个事件,然后看看结果,并且输出信号槽中处理函数的个数。

当然最后再次调用一个输出信号槽的个数只是为了验证对象消失后会不会悲剧,然后最后一个调用thread表示如何给线程中传入参数!当然boost的线程如此简单,以至于让我感觉这是在写线程代码?!今晚看此文章的人,你太苦逼了表示,从13码到14呵呵!



### 如何在VSCode中配置并使用Boost库 为了能够在Visual Studio Code(VSCode)环境中成功编译和运行依赖于Boost库的C++项目,需要完成几个关键设置。 #### 安装Boost库 如果打算利用某些特定组件如`Boost.Filesystem`,则需先构建这些库[^1]。对于仅用于头文件的库部分,则只需解压下载包即可直接投入使用。确保安装版本与目标平台相匹配,比如针对64位应用应采用相应架构下的预编译进制文件或自行编译得到该架构的支持[^5]。 #### 配置开发环境 要在VSCode里集成Boost支持,主要涉及编辑器本身的插件以及任务/调试配置两方面工作: - **扩展安装**: 推荐安装由Microsoft官方维护的C/C++扩展,它提供了 IntelliSense、代码导航等功能。 - **编写tasks.json**: 创建`.vscode/tasks.json`来定义编译命令。下面是一个简单的例子,假设已正确设置了系统的PATH变量指向所需的工具链(例如g++),并且已经通过上述方法获取到了本地路径上的Boost静态链接库(.a/.lib)或者动态链接库(.so/.dll): ```json { "version": "2.0.0", "tasks": [ { "label": "build hello_boost", "type": "shell", "command": "g++", "args": [ "-std=c++17", // 或者其他标准版本 "${workspaceFolder}/main.cpp", "-I/path/to/boost_1_xx_x/", "-L/path/to/libraries/", "-lboost_system-mt", // 如果是多线程模式下使用的系统库 "-o", "${workspaceFolder}/hello_boost" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"], "detail": "compiler launched by build task." } ] } ``` 这里指定了包含路径(`-l`)以便能够找到必要的导入符号表项。 #### 测试程序示例 可以尝试创建一个简单测试案例验证一切正常运作: ```cpp #include <iostream> #include <boost/filesystem.hpp> int main() { namespace fs = boost::filesystem; std::cout << "Current path is: " << fs::current_path().string() << '\n'; return 0; } ``` 此段代码展示了如何引入命名空间简化书写,并调用了来自`Boost.Filesystem`的功能打印当前目录位置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值