Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Core and Concurrency - Languages, Libraries and Development Techniques [closed]

The CPU architecture landscape has changed, multiple cores is a trend that will change how we have to develop software. I've done multi-threaded development in C, C++ and Java, I've done multi-process development using various IPC mechanisms. Traditional approaches of using threads doesn't seem to make it easy, for the developer, to utilize hardware that supports a high degree of concurrency.

What languages, libraries and development techniques are you aware of that help alleviate the traditional challenges of creating concurrent applications? I'm obviously thinking of issues like deadlocks and race conditions. Design techniques, libraries, tools, etc. are also interesting that help actually take advantage of and ensure that the available resources are being utilized - just writing a safe, robust threaded application doesn't ensure that it's using all the available cores.

What I've seen so far is:

  • Erlang: process based, message passing IPC, the 'actor's model of concurrency
  • Dramatis: actors model library for Ruby and Python
  • Scala: functional programming language for the JVM with some added concurrency support
  • Clojure: functional programming language for the JVM with an actors library
  • Termite: a port of Erlang's process approach and message passing to Scheme

What else do you know about, what has worked for you and what do you think is interesting to watch?

like image 742
Kyle Burton Avatar asked Sep 23 '08 15:09

Kyle Burton


5 Answers

I'd suggest two paradigm shifts:

Software Transactional Memory

You may want to take a look at the concept of Software Transactional Memory (STM). The idea is to use optimistic concurrency: any operation that runs in parallel to others try to complete its job in an isolated transaction; if at some point another transaction has been committed that invalidates data on which this transaction is working, the transaction's work is throwed away and the transaction run again.

I think the first widely known implementation of the idea (if not the proof-of-concept and first one) is the one in Haskell : Papers and presentations about transactional memory in Haskell. Many other implementations are listed on Wikipedia's STM article.

Event loops and promises

Another very different way of dealing with concurrency is implemented in the [E programming language](http://en.wikipedia.org/wiki/E_(programming_language%29).

Note that its way of dealing with concurrency, as well as other parts of the language design, is heavily based on the Actor model.

like image 117
Nowhere man Avatar answered Nov 19 '22 17:11

Nowhere man


You mentioned Java, but you only mention threads. Have you looked at Java's concurrent library? It comes bundled with Java 5 and above.

It's a very nice library containing ThreadPools, CopyOnWriteCollections to name a very few. Check out the documentation at the Java Tutorial. Or if you prefer, the Java docs.

like image 9
Steve K Avatar answered Nov 19 '22 17:11

Steve K


Some Scala based stuff:

  • PiLib: A Hosted Language for Pi-Calculus Style Concurrency
  • Event-Based Programming without Inversion of Control
  • Actors that Unify Threads and Events
  • Scala Multicast Actors: Architecture and Implementation
  • Implementing Joins using Extensible Pattern Matching
  • Communicating Scala Objects (Revised)
like image 7
John Nilsson Avatar answered Nov 19 '22 17:11

John Nilsson


I've used processing for Python. It mimicks the API of the threading module and is thus quite easy to use.

If you happen to use map/imap or a generator/list comprehension, converting your code to use processing is straightforward:

def do_something(x):
    return x**(x*x)

results = [do_something(n) for n in range(10000)]

can be parallelized with

import processing
pool = processing.Pool(processing.cpuCount())
results = pool.map(do_something, range(10000))

which will use however many processors you have to calculate the results. There are also lazy (Pool.imap) and asynchronous variants (Pool.map_async).

There is a queue class which implements Queue.Queue, and workers that are similar to threads.

Gotchas

processing is based on fork(), which has to be emulated on Windows. Objects are transferred via pickle/unpickle, so you have to make sure that this works. Forking a process that has acquired resources already might not be what you want (think database connections), but in general it works. It works so well that it has been added to Python 2.6 on the fast track (cf. PEP-317).

like image 6
Torsten Marek Avatar answered Nov 19 '22 17:11

Torsten Marek


Intel's Threading Building Blocks for C++ looks very interesting to me. It offers a much higher level of abstraction than raw threads. O'Reilly has a very nice book if you like dead tree documentation. See, also, Any experiences with Intel’s Threading Building Blocks?.

like image 4
Pat Notz Avatar answered Nov 19 '22 17:11

Pat Notz