你好,游客 登录 注册 搜索
背景:
阅读新闻

C++编程 –安全并发访问容器元素

[日期:2014-09-24] 来源:Linux社区  作者:flyfish1986 [字体:大 中 小]

贴一个简单的示例代码
使用parallel_for_each 算法计算std::array 对象中每个元素的平方
参数分别是lambda 函数、函数对象和函数指针。

#include "stdafx.h"
#include <ppl.h>
#include <array>
#include <iostream>
using namespace Concurrency;
using namespace std;
using namespace std::tr1;


// Function object (functor) class that computes the square of its input.
template<class Ty>
class SquareFunctor
{
public:
 void operator()(Ty& n) const
 {
  n *= n;
 }
};


// Function that computes the square of its input.
template<class Ty>
void square_function(Ty& n)
{
 n *= n;
}
int _tmain(int argc, _TCHAR* argv[])
{
 // Create an array object that contains 5 values.
 array<int, 5> values = { 1, 2, 3, 4, 5 };


 // Use a lambda function, a function object, and a function pointer to
 // compute the square of each element of the array in parallel.


 // Use a lambda function to square each element.
 parallel_for_each(values.begin(), values.end(), [](int& n){n *= n;});


 // Use a function object (functor) to square each element.
 parallel_for_each(values.begin(), values.end(), SquareFunctor<int>());


 // Use a function pointer to square each element.
 parallel_for_each(values.begin(), values.end(), &square_function<int>);


 // Print each element of the array to the console.
 for_each(values.begin(), values.end(), [](int& n) {
  wcout << n << endl;
 });
 return 0;
}

在微软的concurrent_vector.h文件中有这样一句
Microsoft would like to acknowledge that this concurrency data structure implementation
is based on Intel implementation in its Threading Building Blocks ("Intel Material").
也就是微软的concurrent_vector是在Intel 的Threading Building Blocks基础上实现的。

方案3 Intel TBB(Threading Building Blocks)
 Intel TBB 提供的功能
 1 直接使用的线程安全容器,比如 concurrent_vector 和 concurrent_queue。
 2 通用的并行算法,如 parallel_for 和 parallel_reduce。 
 3 模板类 atomic 中提供了无锁(Lock-free或者mutex-free)并发编程支持。

方案4 无锁数据结构支持库Concurrent Data Structures (libcds). 
地址 http://sourceforge.net/projects/libcds/
下载以后里面直接有从VC2008到VC2013的编译环境,依赖于boost库

方案5 Boost 使用boost.lockfree

boost.lockfree实现了三种无锁数据结构:


1 boost::lockfree::queue
2 boost::lockfree::stack
3 boost::lockfree::spsc_queue

生产者-消费者
下面的代码实现的是
实现了一个多写生成,多消费 队列。
产生整数,并被4个线程消费

#include <boost/thread/thread.hpp>
#include <boost/lockfree/queue.hpp>
#include <iostream>


#include <boost/atomic.hpp>


boost::atomic_int producer_count(0);
boost::atomic_int consumer_count(0);


boost::lockfree::queue<int> queue(128);


const int iterations = 10000000;
const int producer_thread_count = 4;
const int consumer_thread_count = 4;


void producer(void)
{
    for (int i = 0; i != iterations; ++i) {
        int value = ++producer_count;
        while (!queue.push(value))
            ;
    }
}


boost::atomic<bool> done (false);
void consumer(void)
{
    int value;
    while (!done) {
        while (queue.pop(value))
            ++consumer_count;
    }


    while (queue.pop(value))
        ++consumer_count;
}


int main(int argc, char* argv[])
{
    using namespace std;
    cout << "boost::lockfree::queue is ";
    if (!queue.is_lock_free())
        cout << "not ";
    cout << "lockfree" << endl;


    boost::thread_group producer_threads, consumer_threads;


    for (int i = 0; i != producer_thread_count; ++i)
        producer_threads.create_thread(producer);


    for (int i = 0; i != consumer_thread_count; ++i)
        consumer_threads.create_thread(consumer);


    producer_threads.join_all();
    done = true;


    consumer_threads.join_all();


    cout << "produced " << producer_count << " objects." << endl;
    cout << "consumed " << consumer_count << " objects." << endl;
}

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/107135.htm

linux
相关资讯       C++编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款