zoukankan      html  css  js  c++  java
  • 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'|

     1 // thread example
     2 #include <iostream>       // std::cout
     3 #include <thread>         // std::thread
     4 
     5 void foo()
     6 {
     7   // do stuff...
     8 }
     9 
    10 void bar(int x)
    11 {
    12   // do stuff...
    13 }
    14 
    15 int main()
    16 {
    17   thread first (foo);     // spawn new thread that calls foo()
    18   thread second (bar,0);  // spawn new thread that calls bar(0)
    19 
    20   std::cout << "main, foo and bar now execute concurrently...
    ";
    21 
    22   // synchronize threads:
    23   first.join();                // pauses until first finishes
    24   second.join();               // pauses until second finishes
    25 
    26   std::cout << "foo and bar completed.
    ";
    27 
    28   return 0;
    29 }

    你在使用<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

     1 #include "LiftOff.h"
     2 
     3 #include <zthread/Thread.h>
     4 
     5 #include <iostream>       // std::cout
     6 
     7 using namespace ZThread;
     8 
     9 int main()
    10 {
    11     try {
    12         Thread th(new LiftOff(10, 1));
    13         std::cout << "waiting for lift off" << std::endl;
    14     } catch (Synchronization_Exception &e) {
    15         std::cerr << e.what() << std::endl;
    16     }
    17 }

    LiftOff.h

     1 #ifndef LIFTOFF_H
     2 #define LIFTOFF_H
     3 
     4 #include <zthread/Runnable.h>
     5 
     6 class LiftOff : public ZThread::Runnable
     7 {
     8     public:
     9         LiftOff(int countDown_, int id_);
    10         ~LiftOff();
    11         void run();
    12     private:
    13         int countDown;
    14         int id;
    15 };
    16 
    17 #endif // LIFTOFF_H

    LiftOff.cpp

     1 #include "LiftOff.h"
     2 
     3 #include <zthread/Runnable.h>
     4 
     5 #include <iostream>
     6 
     7 using namespace std;
     8 
     9 LiftOff::LiftOff(int countDown_, int id_)
    10     :countDown(countDown_), id(id_)
    11 {
    12     // do nothing
    13 }
    14 
    15 LiftOff::~LiftOff()
    16 {
    17     cout << "LiftOff" << id << " destroyed" << endl;
    18 }
    19 
    20 void LiftOff::run()
    21 {
    22     while (countDown--)
    23         cout << id << " count: " << countDown << endl;
    24     cout << id << "LiftOff!" << endl;
    25 }
  • 相关阅读:
    Prometheus实现微信邮件钉钉报警
    产品需求文档和原型
    各类数据集
    redis与mysql数据同步
    hadoop hbase hive spark对应版本
    Redis集群的搭建
    mysql数据库数据与redis同步
    企业级Zabbix监控实战(一)
    mysql实现高可用架构之MHA
    04-爬取单个英雄联盟英雄的符文图片
  • 原文地址:https://www.cnblogs.com/qrlozte/p/4133550.html
Copyright © 2011-2022 走看看