好就没有写自己的技术博客了,信手翻来,发现已是半年前的情形了。漫步网页间,居然搜索到了自己一直关心的咚咚。毕竟还是对C++的那种眷恋,QT也好,TBB也好,Meego也好,都还有有着那份关注。
今天看到的这篇文章是从MSDN里摘录下来的,毕竟ppl还是比较新的。相信国内还有很多人未曾使用过这类东东。以前可以用openmp来做多核计算(其实Intel CPU下TBB更好用些),到了vs 2010里完全可以用微软提供的这个ppl.h来做。长话短说,贴上,看到的咚咚。
转自:http://msdn.microsoft.com/en-us/library/dd554943.aspx
This walkthrough shows how to use the Parallel Tasks andParallel Stacks windows to debug a parallel application. These windows help you understand and verify the runtime behavior of code that uses theTask Parallel Library or theConcurrency Runtime. This walkthrough provides sample code that has built-in breakpoints. After the code breaks, the walkthrough shows how to use theParallel Tasks and Parallel Stacks windows to examine it.
This walkthrough teaches these tasks:
-
How to view the call stacks of all threads in one view.
-
How to view the list of System.Threading.Tasks.Task instances that are created in your application.
-
How to view the real call stacks of tasks instead of threads.
-
How to navigate to code from the Parallel Tasks andParallel Stacks windows.
-
How the windows cope with scale through grouping, zooming, and other related features.
You must have Visual Studio 2010 installed on the computer.
This walkthrough assumes that Just My Code is enabled. On theTools menu, click Options, expand theDebugging node, select General, and then selectEnable Just My Code (Managed only). If you do not set this feature, you can still use this walkthrough, but your results may differ from the illustrations.
C# Sample
If you use the C# sample, this walkthrough also assumes that External Code is hidden. To toggle whether external code is displayed, right-click theName table header of the Call Stack window, and then select or clearShow External Code. If you do not set this feature, you can still use this walkthrough, but your results may differ from the illustrations.
C++ Sample
If you use the C++ sample, you can ignore references to External Code in this topic. External Code only applies to the C# sample.
Illustrations
The illustrations in this topic recorded on a quad core computer running the C# sample. Although you can use other configurations to complete this walkthrough, the illustrations may differ from what is displayed on your computer.
The sample code in this walkthrough is for an application that does nothing. The goal is just to understand how to use the tool windows to debug a parallel application.
To create the sample project
-
In Visual Studio, on the File menu, point to New and then click Project.
-
In the Installed Templates pane, select either Visual C#, Visual Basic, or Visual C++. For the managed languages, ensure that .NET Framework 4 is displayed in the framework box.
-
Select Console Application and then click OK. Remain in Debug configuration, which is the default.
-
Open the .cpp, .cs, or .vb code file in the project. Delete its contents to create an empty code file.
-
Paste the following code for your chosen language into the empty code file.
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <ppl.h>
#include <agents.h>
#include <stdio.h>
#include <concrtrm.h>
#include <vector>
CRITICAL_SECTION cs;
using namespace ::std;
using namespace ::std::tr1;
using namespace ::Concurrency;
task_group task4;
task_group task3;
task_group task2;
volatile long aa = 0;
volatile long bb = 0;
volatile long cc = 0;
static bool waitFor1 = true;
static bool waitFor5 = true;
#pragma optimize("", off)
void Spin()
{
for(int i=0;i<50*50000;++i);
}
#pragma optimize("",on)
template<class Func>
class RunFunc
{
Func& m_Func;
int m_o;
public:
RunFunc(Func func,int o):m_Func(func),m_o(o){
};
void operator()()const{
m_Func(m_o);
};
};
void T(int o)
{
cout << "Scheduled run \n";
};
void R(int o)
{
if (o == 2)
{
while (waitFor5) { ;}
Spin();
//use up all processors but 4 by scheduling 4 non-terminating tasks.
int numProcsToBurn = GetProcessorCount() - 4;
int i;
vector<call<int>*> tasks;
for (i = 0; i < numProcsToBurn; i++)
{
tasks.push_back(new call<int>([](int i){while(true)Spin();}));
asend(tasks[i],1);
cout << "Started task \n";
}
task_handle<RunFunc<decltype(T)>> t6(RunFunc<decltype(T)>(T,i + 1 + 5));
task_handle<RunFunc<decltype(T)>> t7(RunFunc<decltype(T)>(T,i + 2 + 5));
task_handle<RunFunc<decltype(T)>> t8(RunFunc<decltype(T)>(T,i + 3 + 5));
task_handle<RunFunc<decltype(T)>> t9(RunFunc<decltype(T)>(T,i + 4