TBB parallel_for

 

// RepString.cpp : 定义控制台应用程序的入口点。
//

#include <stdio.h>
#include <stdlib.h>
#include <tchar.h>

#include <tbb/parallel_for.h>
#include <tbb/blocked_range.h>
#include <tbb/tbb_allocator.h>
#include <tbb/task_scheduler_init.h>
using namespace tbb;

char Foo(char pChar)
{
   
    if(pChar>=65 && pChar <= 90)
    {
        return (char)((int)(pChar) + 32);
    }
    else if(pChar>=97 && pChar <= 122)
    {
        return (char)((int)(pChar) - 32);
    }
    return 0;

  
}

class ApplyFoo
{
    char *const my_a;
public:
    void operator()( const blocked_range<size_t> &r) const
    {
        char *a = my_a;
        for(size_t i=r.begin();i!=r.end();++i)
        {
            char temp = Foo(a[i]);
            printf("%c\t",temp);
        }
    };

    ApplyFoo(char a[])
        :my_a(a)
    {}

};

void ParallelApplyFoo(char a[],size_t n)
{
    parallel_for(blocked_range<size_t>(0,n),ApplyFoo(a),auto_partitioner());
}

int _tmain(int argc, _TCHAR* argv[])
{
    int nthread = strtol(argv[0],0,0);
    task_scheduler_init init(task_scheduler_init::deferred);
    if(nthread>=1)
    {
        init.initialize(nthread);
    }
   
    char ArrayChar[] = {'O','G','d','R','R','F','A'};
    int len = strlen(ArrayChar);
    ParallelApplyFoo(ArrayChar,len);
 

 return 0;
}

 

你可能感兴趣的:(c,Class,parallel)