views:

2314

answers:

7

I keep on hearing about concurrent programing every where. Can you guys throw some light on what it's and how c++ new standards facilitate doing the same?

+2  A: 

Perhaps this video might help shine some light for you :-)
http://channel9.msdn.com/posts/Charles/The-Concurrency-Runtime-Fine-Grained-Parallelism-for-C/

Joel Martinez
+2  A: 

Concurrency is having multiple threads of execution for a given process. As of today, C++ does not directly support it. However, several libraries exist that will tie a given function to a new thread of execution. The Unix standard is the pthreads library.

Paul Nathan
The Posix standard (not Unix)
Martin York
+12  A: 

Concurrency is about your code doing multiple things at the same time. This is typically done with explicit "threads", but there are other possibilities. For example, if you use OpenMP directives in your code then a compiler that supports OpenMP will automatically generate threads for you.

Thread is short for "thread of execution". In a single-threaded C++ program, execution starts at main(), and then proceeds in a sequential fashion. In a multi-threaded program, the first thread starts at main, but additional threads may be started by the application which start at a user-specified function. These then run concurrently, or in parallel with the original thread.

In C++0x threads are started using the std::thread class:

void my_function()
{
    // do stuff
}
std::thread my_thread(my_function); // run my_function in its own thread

The new C++0x standard also supports:

  • atomic values and operations with the std::atomic<> class template,
  • mutexes for data protection (std::mutex, std::recursive_mutex, etc.)
  • lock classes for ease of managing lock lifetime (std::lock_guard<>, std::unique_lock<>)
  • std::lock and std::try_lock functions to manage acquiring multiple locks at the same time without risking deadlock
  • condition variables to ease waiting for an event (std::condition_variable, std::condition_variable_any)
  • futures, promises and packaged tasks to simplify passing data between threads, and waiting for a value to be ready. This addresses the classic "how do I return a value from a thread" question.
  • thread-safe initialization of local static objects
  • the thread_local keyword to declare thread-local data

I gave a more detailed overview of the new C++0x thread library in my article on devx.com: Simpler Multithreading in C++0x

I write about multithreading and concurrency in C++ on my blog. I'm also writing a book on the topic: C++ Concurrency in Action.

Anthony Williams
+12  A: 

When you say "how c++ new standards facilitate" concurrent programming, I assume you're talking about the soon (?) to be released C++09 standard.

The new standard as it currently stands in draft form supports the following items that help with concurrent programming:

  • atomic types and addresses
  • a thread class
  • thread_local storage (which was just added into the draft standard a few months ago)
  • mutual exclusion (mutex classes)
  • condition variables - this is particularly nice for Windows, since condition variables are difficult to implement correctly in Win32. This means that eventually Microsoft should provide support for condition variables at least in the MSVC++ runtime, so it will be easy to get correct condition variable semantics on WIn32.
Michael Burr
Has anybody written a good summary about these features yet?
Martin York
I think they're heavily based on boost::thread so there shouldn't be much difference between that documentation and the new standard.
gbjbaanb
Unfortunately, it's probably going to be C++10. Other than that, good post.
coppro
Yeah - I guess I'm trying to be optimistic; Maybe instead of C++0x it should start to be called C++xx. If nothing else it has a nice look to it.
Michael Burr
It'll just be C++0A. ;) We're programmers, don't have to stick to decimal! :D
KTC
+1  A: 

My slightly different take, specific to future directions of programming paradigms:

Concurrency is about writing your program such that it can be doing multiple things at once if the hardware supports it. Currently, most languages have fairly heavy and complicated mechanisms to allow the programmer to specify this (eg: threads with manual synchronization, OpenMP pre-processor directives, etc.).

As hardware improves, it's going to improve horizontally (more cores) rather than vertically (faster single core). This means apps will need to have "latent concurrency" in order to scale with "faster" hardware. Languages are currently trying to evolve to best support this, to be in the position of best language for future development.

C++0x is adding more built-in support for the "old" methods of programming concurrency. Various compiler vendors are adding "new" methods which abstract the threading model and allow run-time decisions on numbers of threads, etc. (based on the hardware of the machine); for Microsoft in particular, see F#, concurrency runtime, parallel extensions, etc.

Hope that helps.

Nick
+3  A: 

C++CSP2 - Easy Concurrency for C++

http://www.cs.kent.ac.uk/projects/ofa/c++csp/

CSP is a based on a proper concurrent paradigm as opposed to threads and locks and all other manner of things which are tacked on as an afterthought.

(See Occam-Pi for a concurrent programming language (also based on CSP))

Dynite
+2  A: 

Articles:Check thses articles to know about concurrency

  1. Free lunch is over
  2. Selected articles by herbsutter
  3. Link to pdf
yesraaj