C++并发编程超简单示例

C++下的并行编程,按照我的理解:
1. 写好一个待并行计算的函数f1();
2. thread t(f1), 并行开始;
3. f2 放在主函数中,即可实现f1和f2的并行运行;

thread t(f1);// 并行开始
f2();
t.join();// 并行结束

记得开头

 #include <thread>

并行示例

// MultiThread.cpp : 并行测试程序
//

#include "stdafx.h"
#include 
#include 
#include 
#include 

using namespace std;
int N = 10;


void thread_task()  //用于比较的函数
{
    long int sum2 = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            sum2 += (i + j);
        cout << "thread1: " << i << endl;
    }
    cout << "sum2: " << sum2 << endl;
}

int main()
{
    long int sum1 = 0;
    int j = 0;
    clock_t  clockBegin, clockEnd;

    clockBegin = clock(); // 计时开始

    thread t(thread_task);// 并行开始
    //thread_task();  //串行开始

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            sum1 += (i + j);
        cout << "thread2: " << i << endl;
    }
    t.join();// 并行结束

    cout << "sum1: " << sum1 << endl;

    clockEnd = clock();  // 计时结束

    cout << "time is " << (clockEnd - clockBegin) << " ms" << endl;
    system("pause");
    return 0;
}


并行结果:可见线程1、2是不分顺序进行的,很好的体现了并行特性。

C++并发编程超简单示例_第1张图片

串行与并行耗时计算

// MultiThread.cpp : 并行计时程序

#include "stdafx.h"
#include 
#include 
#include 
#include 

using namespace std;
int N = 10000;


void thread_task()  //用于比较的函数
{
    long int sum2 = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            sum2 += (i + j);
        //cout << "thread1: " << i << endl;
    }
    cout << "sum2: " << sum2 << endl;
}

int main()
{
    long int sum1 = 0;
    int j = 0;
    clock_t  clockBegin, clockEnd;

    clockBegin = clock(); // 计时开始

    thread t(thread_task);// 并行开始
    //thread_task();  //串行开始

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            sum1 += (i + j);
        //cout << "thread2: " << i << endl;
    }
    t.join();// 并行结束

    cout << "sum1: " << sum1 << endl;

    clockEnd = clock();  // 计时结束

    cout << "cocurrent cal time is " << (clockEnd - clockBegin) << " ms" << endl;
    system("pause");
    return 0;
}

// MultiThread.cpp : 串行计时程序

#include "stdafx.h"
#include 
#include 
#include 
#include 

using namespace std;
int N = 10000;


void thread_task()  //用于比较的函数
{
    long int sum2 = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            sum2 += (i + j);
        //cout << "thread1: " << i << endl;
    }
    cout << "sum2: " << sum2 << endl;
}

int main()
{
    long int sum1 = 0;
    int j = 0;
    clock_t  clockBegin, clockEnd;

    clockBegin = clock(); // 计时开始

    //thread t(thread_task);// 并行开始
    thread_task();  //串行开始

    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
            sum1 += (i + j);
        //cout << "thread2: " << i << endl;
    }
    //t.join();// 并行结束

    cout << "sum1: " << sum1 << endl;

    clockEnd = clock();  // 计时结束

    cout << "serial computing time is " << (clockEnd - clockBegin) << " ms" << endl;
    system("pause");
    return 0;
}

这里写图片描述
这里写图片描述

你可能感兴趣的:(C++并发编程超简单示例)