PostHeaderIcon Hello Thread In Cplusplus

I’m sorry. I have not update this website for a long time~

Things have to do, but this is not my pretense.OK let’s begin, I will introduce something about C++ concurrency!

C++0x will come soon, so we will follow with the new standard edition for C++.

Thread is one of the new libraries in it.

First,begin with a simple hello world example:

#include <iostream>
int main()
{  
    std::cout<<"Hello World\n";
    return 0;
}

Now add the thread head file for it. and also show below(hello.cpp):

#include <iostream>
#include <thread>
void hello()
{ 
    std::cout<<"Hello Concurrent World!\n";
}
int main()
{
    std::cout<<"Hello main world!\n";
    std::cout<<"Now let's begin our first thread!\n";
    std::thread t(hello);
    t.join();
    return 0;
}

but how to build it? you hava to use g++ in linux as follow:

g++ -Wall -g -std=c++0x -pthread hello.cpp

To run it is really simple:

./a.out

The output is as follows:

Hello main world!
Now let’s begin our first thread!
Hello Concurrent World!

Simple, right? and also excited,C++ really support the thread.

Good news for C++ programmer, certainly for me!

I’ll introduce thread in recent days!

14,990 views

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


Copyright © 2010 - C++ Technology. All Rights Reserved.

Powered by Jerry | Free Space Provided by connove.com