visual+c+++2010环境下调试并行开发程序

本文档详细介绍了如何在Visual C++ 2010环境下使用Parallel Tasks和Parallel Stacks窗口调试并行应用程序。通过示例代码和逐步指导,展示了如何查看所有线程的调用堆栈、任务列表,以及如何导航和理解运行时行为。适合于了解并使用Task Parallel Library或Concurrency Runtime的开发者。
摘要由CSDN通过智能技术生成

好就没有写自己的技术博客了,信手翻来,发现已是半年前的情形了。漫步网页间,居然搜索到了自己一直关心的咚咚。毕竟还是对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

  1. In Visual Studio, on the File menu, point to New and then click Project.

  2. 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.

  3. Select Console Application and then click OK. Remain in Debug configuration, which is the default.

  4. Open the .cpp, .cs, or .vb code file in the project. Delete its contents to create an empty code file.

  5. 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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值