C++11 多线程

C++11开始支持多线程编程,之前多线程编程都需要系统的支持,在不同的系统下创建线程需要不同的API如pthread_create(),Createthread(),beginthread()等,使用起来都比较复杂,C++11提供了新头文件<thread>、<mutex>、<atomic>、<future>等用于支持多线程。

使用C++11开启一个线程是比较简单的,下面来看一个简单的例子:

#include <thread>
#include <iostream>
using namespace std;

void hello()
{
    cout << "Hello from thread " <<endl;
}
int main()
{
    thread t1(hello);//参数是这个线程执行的回调函数的地址
    t1.join();//通过jion()方法阻塞主线程,直到t1线程执行结束为止
    cout<<"Main Thread"<<endl;
    return 0;
}

结果:

Hello from thread
Main Thread

Process returned 0 (0x0)   execution time : 5.330 s
Press any key to continue.

以上程序通过thread类直接声明一个线程t1,参数是这个线程执行的回调函数的地址,通过jion()方法阻塞主线程,直到t1线程执行结束为止。

C++11支持Lambda表达式,因此一个新线程的回调函数也可以是有一个Lambda表达式的形式,但是注意如果使用Lambda表达式最好不要使用引用的方式,应该使用值传递的方式来访问数据,在多线程中使用引用容易造成混乱。下面这个例子稍微复杂,创建了多个子线程,并使用了get_id()方法来获取当前线程的id。

#include <thread>
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    vector<thread> threads;
    for(int i = 0; i < 5; ++i)
    {
        threads.push_back(
            thread([](){cout<<"Hello from lamda thread " <<this_thread::get_id()<<endl;})
        );
    }
    for(auto& thread:threads)
    {
        thread.join();
    }
    cout<<"Main Thread"<<"\t"<<this_thread::get_id()<<endl;

    return 0;
}

上述代码中,使用vector来存放每个线程,线程的回调函数通过Lambda表达式产生,注意后面join的使用方式。
结果:

Hello from lamda thread 2
Hello from lamda thread 3
Hello from lamda thread 4
Hello from lamda thread 5
Hello from lamda thread 6
Main Thread     1

Process returned 0 (0x0)   execution time : 0.493 s
Press any key to continue.

使用多线程的程序中操作共享数据的时候一定要小心,由于线程的乱序执行,可能会得到意想不到的结果。因此要加锁。

#include <thread>
#include <iostream>
#include <vector>
#include <mutex>
using namespace std;

struct Counter {
    mutex mutex;
    int value;
    Counter():value(0){}
    void increment()
    {
       mutex.lock();//(1)使用锁
        ++value;
       mutex.unlock();//(2)解锁
    }
    void decrement()
    {
        mutex.lock();
        --value;
        mutex.unlock();
    }
};

int main()
{
    Counter counter;
    vector<thread> threads;
    for(int i = 0; i < 5; ++i){
        threads.push_back(thread([&](){
            for(int i = 0; i < 100; ++i){
                counter.increment();
            }
        }));
    }
    for(auto& thread : threads){
        thread.join();
    }
    cout << counter.value << endl;
    return 0;
}

由于创建线程是使用lambda表达式,并使用引用的方式访问counter这个变量,当没有使用lock来保护的时候,执行的结果可能不像预期的500(程序的意思是每个线程使counter中的value自加100次,5个线程运行结束的时候应该是500),当没有使用锁的时候自加的操作可能被其他线程打断,因此结果可能会小于500。

转自http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值