C++11 Thread (the Zthread library I'm currently using)

Source: Internet
Author: User

To date (2014-11-30), GCC has essentially fully supported all c++11 functions, and in fact, since GCC4.7,-std=c++11 options have been supported, and-STD=C++0X options have been supported before the 4.7 release.

But now that the MinGW Working Group is not keeping pace with the GNU GCC team, in fact GCC has now been updated to 4.9, and the latest version of the Mingw-installer can be obtained in g++ It also stays in the 4.8.1 version, not only so, although 4.8.1,MINGW does not provide many of the functions c++11 should have. (That is, your use of GCC on a non-Windows system can already fully support the full functionality of c++11, but not yet on windows, at least for now).

For example, <regex> and <THREAD>,MINGW are not providing

Here is a copy from http://www.cplusplus.com/reference/thread/thread/a routine, in the compilation of mingw-gcc-4.8.1 will prompt the following error

Main.cpp|17|error: ' Thread ' is not a member of ' STD ' |

1 //Thread Example2#include <iostream>//Std::cout3#include <thread>//Std::thread4 5 voidfoo ()6 {7   //Do stuff ...8 }9 Ten voidBarintx) One { A   //Do stuff ... - } -  the intMain () - { -Thread First (foo);//spawn new thread that calls Foo () -Thread second (bar,0);//spawn new thread that calls bar (0) +  -Std::cout <<"Main, foo and bar now execute concurrently...\n"; +  A   //Synchronize threads: atFirst.join ();//pauses until first finishes -Second.join ();//pauses until second finishes -  -Std::cout <<"foo and bar completed.\n"; -  -   return 0; in}

When you use <regex>, there will be such compiler or linker error. It is because MinGW does not fully provide the implementation of these functions (I think it is better not to offer a head like <thread> or <regex> directly, you are not misleading users).

Wait for the MinGW update AH.

Here's a look at the use of the Zthread Library under Windows (Tutorial reference its documentation, or thinking in C + + vol2)

Reference: http://www.cnblogs.com/moodlxs/archive/2012/10/16/2725329.html

: http://zthread.sourceforge.net (i downloaded 2.3.2), below I use zthread_home to represent the downloaded Zthread folder, for example f:/ ZThread-2.3.2, which contains doc,include,src and so on several subfolders

Before compiling:

Read the Readme and quote as follows:

Compiling:

Zt_posix, ZT_WIN32 should is 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 the flags out Zthreads would try to guess.

See BUILDING for more details.

To view building, I quote as follows:

HOWTO COMPILE the SOURCE:

There is several options for compiling the source code.

* The preferred option is to use the Configure script, which is provided.
I can only actively maintain one method for compiling and configuring
The source.

./configure--help would provide a list of options detailing this method.

* Any and other method are up to you. There is simply too many compilers,
Too many versions and too many platforms to maintain separate build files
For.

But the this doesn ' t mean is out of luck.

I have structured the code so it's very simple to compile the library
However suits your needs best. All your need to does 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 are specific to your compiler and I leave the
Details up-to-you.

Anything want to tweak configuration-wise can is done by editing
Include/zthread/config.h

Compile: I choose to compile as SLL (Static Link Library), that is. A or. lib file, the steps are as follows

    • In order for the g++ compiler to find the desired header file, first copy everything under Zthread_home/include (actually a Zthread folder and CVS folder) to Mingw_home/include
    • Open terminal, switch to ZTHREAD_HOME/SRC, use command: g++-c *. CXX compiles all of the cxx files (but does not link) to get a series of. o Files (many of which warning, considering that Zthread is a library that stopped updating in 2005, so warning is directly ignored, but there are several error, according to the error prompt, use the command: G + +-fpermissive-c *.cxx compilation passed)
    • Then use the command: Ar-r zthread_win32.a *.O package into a library file, and then put the zthread_win32.a into the Zthread_home/bin directory (if not, create a bin folder)

Configuration: (Note that both debug and release are set)

    • Open a project in codeblocks, such as my test, select Project>build options>linker Settings
    • Click Add to add zthread_home/bin/zthread_win32.a
    • Then select Search Directories>compiler
    • Click Add to add Zthread_home/include

Routines:

Main.cpp

1#include"LiftOff.h"2 3#include <zthread/Thread.h>4 5#include <iostream>//Std::cout6 7 using namespaceZthread;8 9 intMain ()Ten { One     Try { AThread th (NewLIFTOFF (Ten,1)); -Std::cout <<"waiting for lift off"<<Std::endl; -}Catch(Synchronization_exception &e) { theStd::cerr << e.what () <<Std::endl; -     } -}

LiftOff.h

1 #ifndef liftoff_h2 #defineLiftoff_h3 4#include <zthread/Runnable.h>5 6 classLIFTOFF: Publiczthread::runnable7 {8      Public:9LIFTOFF (intCountdown_,intid_);Ten~LiftOff (); One         voidrun (); A     Private: -         intCountdown; -         intID; the }; -  - #endif //Liftoff_h

LiftOff.cpp

1#include"LiftOff.h"2 3#include <zthread/Runnable.h>4 5#include <iostream>6 7 using namespacestd;8 9Liftoff::liftoff (intCountdown_,intid_)Ten : Countdown (Countdown_), id (id_) One { A     // do nothing - } -  theliftoff::~LiftOff () - { -cout <<"LiftOff"<< ID <<"destroyed"<<Endl; - } +  - voidLiftoff::run () + { A      while(countdown--) atcout << ID <<"Count:"<< Countdown <<Endl; -cout << ID <<"liftoff!"<<Endl; -}

C++11 Thread (the Zthread library I'm currently using)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.