Click here to Skip to main content
15,886,813 members
Articles / Desktop Programming / Win32

Concurrency Runtime in Visual C++ 2010

Rate me:
Please Sign up or sign in to vote.
4.98/5 (60 votes)
11 Nov 2010CPOL49 min read 147.8K   2.2K   164  
Learn about parallel algorithms, parallel containers, tasks, task groups, agents library, task scheduler etc in VC10
// Copyright (C) 2010
// Ajay Vijayvargiya

// This source code was published on CodeProject.com, under CPOL license
// This code CANNOT be used for similar publication on the web or as printed material.
// This can, however, be used as educational reference in educational institutions.
// You are allowed to use the accompanying code in your programs, 
//    commercial or non commercial. 

// This source code is only intended to explicate the Concurrency Runtime in Visual C++ 2010,
// and thus may contain bugs, some logical issues etc.
// The explanation is relevant to Visual C++ 2010 (Compiler version: 16.0)

// http://www.codeproject.com/KB/cpp/parallelcpp.aspx


// For performance benchmarking, simple approach, GetTickCount is used.
// If you prefer, you may use other high performance counters.
// Ignore the importance and non-optimized code, they are just for illustration.



#include "stdafx.h"
#include "examples.h"
#include <iostream>
#include <concrt.h>

// Uncomment following line to enable Debugging in Debug build
 #define _DEBUG_RUNNABLE 1


/************************************************************************/
/* Console Application for illustrating the Concurrency RunTime         */
/* Update History of this Code (project):								*/
/* Sept 1, 2009	First version released,									*/
/*				parallel_for, parallel_for_each, parallel_invoke		*/
/*				and combinable class demonstrated.						*/
/************************************************************************/


/*  Warnings Disabled:

	4244 : parallel_for has some bug when ULONGLONG parameter is passed.
           In short: it deduces number of CPUs needed to put for execution 
  		   and says "possible loss of data" - Can there be ULONGLONG' CPUS? 

*/


using namespace std;

int _tmain()
{
#if defined(_DEBUG) && !defined(_DEBUG_RUNNABLE)
	
	wcout << "Please run this sample with Release build. Debug build will not give "
		"optimum performance for small programs. (Or #define _DEBUG_RUNNABLE 1 to enable debugging).";

	return -1;

#endif


	// --------------------- PROGRAM STARTS HERE ---------------------


	// Ensure that concurrency runtime gets activated.
	// The is NOT mandatory, and is just one of the methods.
	// When you call ANY function/method for parallel execution,
	// the CR initializes itself.

	// The following call is needed to give correct timing, otherwise
	// the first call to CR (like parallel_for), would take some time to 
	// initialize CR and thus timing may be incorrect.
	Concurrency::CurrentScheduler::Get();

	int nChoice;

	wcout << "Sample Console Application to demonstrate Concurrency Runtime." <<
		"\nPlease select one of the options:\n";

	wcout << "\n\t1. Sum of numbers in Parallel\n\t   (parallel_for, combinable).";
	wcout << "\n\t2. Find primes from vector in Parallel\n\t   (parallel_for_each, combinable).";
	wcout << "\n\t3. Find even, odd and primes \n\t   (parallel_invoke).";



	wcout << "\n\n -- Your option: ";
	wcin >> nChoice;


	switch (nChoice)
	{
	case 1:
		ParallelSum();
		break;

	case 2:
		ParallelPrimeFind();
		break;

	case 3:
		ParallelInvokeExample();
		break;

	default:

		wcout << "\n**Not valid option...\n";
	}
	
	wcout << "\n ** Done. Press Enter to exit";
		
	wcin.ignore ( 80, '\n' );		// DONT ASK ME HOW DOES THIS WORK - I found this as quick solution
	wcin.get();	
	
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Started programming with GwBasic back in 1996 (Those lovely days!). Found the hidden talent!

Touched COBOL and Quick Basic for a while.

Finally learned C and C++ entirely on my own, and fell in love with C++, still in love! Began with Turbo C 2.0/3.0, then to VC6 for 4 years! Finally on VC2008/2010.

I enjoy programming, mostly the system programming, but the UI is always on top of MFC! Quite experienced on other environments and platforms, but I prefer Visual C++. Zeal to learn, and to share!

Comments and Discussions