TBB的一个简单示例

时间:2015-03-02 22:18:47

标签: c++ tbb

我试图进入线程构建模块,但我无法克服我遇到的错误。

我在这种情况下尝试做的是表明在尝试查找素数时多线程更好。如何使用parallel_for对循环进行并行化?

#include <iostream>
#include <time.h>
using namespace std;

bool isPrime(int i){
     for(int k=2;k<=i/2;k++)if(i%k==0)return 0;
     return 1;
     }

int main(){
    clock_t t1,t2;
    t1 = clock();
    int counter = 0;
    int number = 2;
    int limit;

    cout << "Input which prime you want: ";
    cin >> limit;

    while(true){
        if(isPrime(number))counter++;
        if(counter == limit)break;
        number++;
    }

    t2 = clock();
    double timeElapsed = ((double) t2 - (double) t1) / CLOCKS_PER_SEC;
    cout << limit << "-nth prime is " << number << endl;
    cout << "Time time elapsed: " << timeElapsed << " s." << endl;
}

0 个答案:

没有答案