c++11 thread (目前我使用的ZThread库)
目前为止(2014-11-30),GCC其实已经基本上完全支持C++11的所有功能了,事实上从GCC4.7之后,就支持了-std=c++11选项,在4.7版本之前,也开始支持-std=c++0x的选项了
但是目前由于MinGW工作组的问题(没有跟上GNU GCC工作组的步伐,事实上目前GCC已经更新到4.9了,MinGW-Installer中能获取的最新版本的G++还停留在4.8.1版本,不仅如此,尽管是4.8.1,MinGW并没有提供很多C++11应有的功能)。(也就是说,你在非Windows系统上使用GCC已经可以完全支持C++11的全部功能了,但是在Windows上,至少目前还不能)。
比如说<regex>和<thread>,MinGW都没有提供实现
下面是从http://www.cplusplus.com/reference/thread/thread/拷贝过来的一个例程,在MinGW-GCC-4.8.1的编译的时候会提示如下的错误
main.cpp|17|error: 'thread' is not a member of 'std'|
// thread example
#include <iostream> // std::cout
#include <thread> // std::thread void foo()
{
// do stuff...
} void bar(int x)
{
// do stuff...
} int main()
{
thread first (foo); // spawn new thread that calls foo()
thread second (bar,); // spawn new thread that calls bar(0) std::cout << "main, foo and bar now execute concurrently...\n"; // synchronize threads:
first.join(); // pauses until first finishes
second.join(); // pauses until second finishes std::cout << "foo and bar completed.\n"; return ;
}
你在使用<regex>的时候也会出现这样那样的compiler或者linker的报错。其实就是因为MinGW并没有完全提供对这些功能的实现(我觉得与其这样,还不如直接就不提供诸如<thread>或者<regex>的头,你这不是误导用户么)。
坐等MinGW的更新啊。
下面说一下Windows下,ZThread库的使用(教程参考其文档,或者Thinking in C++ vol2)
参考资料:http://www.cnblogs.com/moodlxs/archive/2012/10/16/2725329.html
下载地址:http://zthread.sourceforge.net (我下载的是2.3.2),下面我用ZTHREAD_HOME代表下载后的Zthread文件夹,例如F:/ZThread-2.3.2,里面包含doc,include,src等等几个子文件夹
在编译之前:
阅读README,引用如下:
COMPILING:
ZT_POSIX, ZT_WIN32 should be defined to specific what platofrm your building
on this. This is mainly so the right modifiers can be placed on class and
function definitions to export things from a DLL properly if your using the
WIN32 implementation. If you leave these flags out ZThreads will try to guess.See BUILDING for more details.
查看BUILDING,我引用如下:
HOWTO COMPILE THE SOURCE:
There are several options for compiling the source code.
* The preferred option is to use the configure script that is provided.
I can only actively maintain one method for compiling and configuring
the source../configure --help will provide a list of options detailing this method.
* Any other method is up to you. There are simply too many compilers,
too many versions and too many platforms to maintain separate build files
for.BUT, this doesn't mean you are out of luck.
I have structured the code so that it is very simple to compile the library
however suits your needs best. All you need to do is include the .cxx files
in the src/ directory (not the subdirectories) in your build rule, and add the
include directory to your compilers include/ path.Anything else you need to do is specific to your compiler and I leave the
details up to you.Anything you want to tweak configuration-wise can be done by editing
include/zthread/Config.h
编译:我选择编译为SLL(Static Link Library),也就是.a或者.lib文件,步骤如下
- 为了让g++编译器能找到所需的头文件,先把ZTHREAD_HOME/include下的所有东西(其实就是一个zthread文件夹和CVS文件夹)拷贝到MinGW_HOME/include中
- 打开terminal,切换到ZTHREAD_HOME/src,使用命令:g++ -c *.cxx编译所有的cxx文件(但不链接)得到一系列.o文件(其中有很多Warning,考虑到ZThread毕竟是2005年就停止更新的库了,所以warning就直接无视,但是有几个error,根据error的提示,使用命令:g++ -fpermissive -c *.cxx编译通过)
- 然后使用命令:ar -r zthread_win32.a *.o打包成库文件,然后把zthread_win32.a放到ZTHREAD_HOME/bin目录下(没有的话就创建一个bin文件夹)
配置:(注意,Debug和Release都要设置)
- 打开CodeBlocks的一个项目,例如我的test,选择Project>Build Options>Linker Settings
- 点击Add,添加ZTHREAD_HOME/bin/zthread_win32.a
- 然后选择Search directories>Compiler
- 点击Add,添加ZTHREAD_HOME/include
例程:
main.cpp
#include "LiftOff.h" #include <zthread/Thread.h> #include <iostream> // std::cout using namespace ZThread; int main()
{
try {
Thread th(new LiftOff(, ));
std::cout << "waiting for lift off" << std::endl;
} catch (Synchronization_Exception &e) {
std::cerr << e.what() << std::endl;
}
}
LiftOff.h
#ifndef LIFTOFF_H
#define LIFTOFF_H #include <zthread/Runnable.h> class LiftOff : public ZThread::Runnable
{
public:
LiftOff(int countDown_, int id_);
~LiftOff();
void run();
private:
int countDown;
int id;
}; #endif // LIFTOFF_H
LiftOff.cpp
#include "LiftOff.h" #include <zthread/Runnable.h> #include <iostream> using namespace std; LiftOff::LiftOff(int countDown_, int id_)
:countDown(countDown_), id(id_)
{
// do nothing
} LiftOff::~LiftOff()
{
cout << "LiftOff" << id << " destroyed" << endl;
} void LiftOff::run()
{
while (countDown--)
cout << id << " count: " << countDown << endl;
cout << id << "LiftOff!" << endl;
}
c++11 thread (目前我使用的ZThread库)的更多相关文章
- 漫谈C++11 Thread库之原子操作
我在之前一篇博文<漫谈C++11 Thread库之使写多线程程序>中,着重介绍了<thread>头文件中的std::thread类以及其上的一些基本操作,至此我们动手写多线程程 ...
- c++11 thread的学习
http://www.cnblogs.com/wxquare/p/6736202.html 还没开始 留个链接 使用c++11 thread支持实现 一个生产者消费者模型 下面是一个生产者消费者问题 ...
- c++ 11 thread 初试
最新的 c++11标准整合进了 线程支持.以下写一个小程序測试一下. 測试代码: #include <iostream> #include <thread> void hell ...
- 在Windows下使用nmake+Makefile+编译ZThread库(附例子)
----------2015/01/09/23:21更新----------------------------------- 关于保留DEBUG信息的一个简单例子,见这篇随笔 ----------2 ...
- 在Window和Linux下使用Zthread库
ZThread库是一个开源的跨平台高级面向对象的线性和sycnchronization 库,以运行POSIX 和Win32 系统中的C++程序. ZThread库的主页:http://zthread. ...
- (原创)发布一个c++11开发的轻量级的并行Task库TaskCpp
TaskCpp简介 TaskCpp是c++11开发的一个跨平台的并行task库,它的设计思路来源于微软的并行计算库ppl和intel的并行计算库tbb,关于ppl和tbb我在前面有介绍.既然已经有了这 ...
- 在Window和Linux下使用Zthread库(跨平台高级面向对象的线性和sycnchronization 库)
ZThread库是一个开源的跨平台高级面向对象的线性和sycnchronization 库,以运行POSIX 和Win32 系统中的C++程序. ZThread库的主页:http://zthread. ...
- 漫谈c++11 Thread库之使写多线程程序
c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...
- 并发编程: c++11 thread(Func, Args...)利用类成员函数创建线程
c++11是VS2012后支持的新标准,为并发编程提供了方便的std::thread. 使用示例: #include <thread> void thread_func(int arg1, ...
随机推荐
- [转]SQL Server 创建数据库邮件
本文转自:http://www.cnblogs.com/gaizai/p/3358958.html 一. 背景 数据库发邮件通知数据库的运行状态(状态可以通过JOB形式获取)和信息,达到预警的效果. ...
- OpenWRT DNS无法解析WAN连接的内网服务器域名
系统版本OpenWrt Chaos Calmer 15.05.1,网络连接为:WAN口连接内网10.x.x.x网段,WAN口设置为静态IP.设置L2TP接口,通过L2TP访问外网.问题出现于,所有外网 ...
- Wishbone B3总线Generic RAM写法
以下Verilog HDL代码符合wishbone总线B3标准协议,在Altera和Xilinx的开发工具上可以实现综合,自动推断并采用片上RAM资源,可以完成内存内容的初始化. /* ******* ...
- after、append和appendTo三个函数的区别
jq文档的说明是 1.after函数 定义和用法: after() 方法在被选元素后插入指定的内容. 语法: $(selector).after(content) 实例: <html>&l ...
- [Android Pro] 查看 keystore文件的签名信息 和 检查apk文件中的签名信息
1: 查看 keystore文件的签名信息 keytool -list -v -keystore keystoreName -storepass keystorePassword 2: 检查apk文件 ...
- Java经典算法汇总之冒泡排序
冒泡排序基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们的排序与排序要求相反时 ...
- Linux程序存储结构与进程结构 堆和栈的差别
摘要:本文主要讲述了Linux系统中.程序存储结构(代码区.数据段和BBS区)与进程的基本结构(代码区.数据段.BBS区.堆和栈).以及堆和栈的差别. Linux程序存储结构与进程结构 1.Linux ...
- oracle update left join查询
对于有的更新语句,要更新的表可能条件不够,需要用到left join关联其他表, 但是不能直接关联,否则报错:错误如下: update imim_gireqbillitems gi left join ...
- hibernate 关系映射之 单向外键关联一对一
这里的关系指的是对象与对象之间的关系 注解方式单向关联一对一: //这个类描述的husband是一个对应一个wife的 import javax.persistence.Entity; import ...
- Ubuntu 18.04修改IP地址
注:配置/etc/network/interfaces已无用 root@ubuntu:~# vim /etc/netplan/50-cloud-init.yaml network: ethernets ...