Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What type of multithreading would be best to learn?

I want to learn multi-threading in C++ but I'm not sure what type would be most useful. The ones I've seen tutorials on are:

  • Windows native calls
  • OpenMP
  • Boost

(I'm sure that there are probably more.)

What is each one's key features and what are they best used for?

Note: I've already done some multi-threading in C# by manually creating the threads, and more complexity of the threading will just make it more fun. :)

like image 328
QuantumKarl Avatar asked Apr 22 '11 12:04

QuantumKarl


People also ask

Which collection is best for multithreading?

The single threaded collections are still better to use if you can ensure that only one threaded will every access that collection.

Is multithreading easy to learn?

Multi-threaded programming is probably the most difficult solution to concurrency. It basically is quite a low level abstraction of what the machine actually does. There's a number of approaches, such as the actor model or (software) transactional memory, that are much easier.

Which language is best for multithreading?

It even has software transactional memory. All in all, Clojure goes to great lenght to help you write correct multithreaded and concurrent code.

What are the types of multithreading?

There are three models in multithreading: Many to many model, Many to one model, and one to one model.


2 Answers

I'd start with pthreads if you have more of a C background, or Boost Thread if you are accustomed to more idiomatic C++. Either is reasonably portable and widely used.

like image 52
John Zwinck Avatar answered Oct 07 '22 09:10

John Zwinck


How about TBB? It is portable and has easy to use parallel template patterns, concurrent containers, task scheduler and scalable memory allocaturs. TBB will let you manage threads directly, but that is not necessary in most of the cases.

Personally I would stay away from platform specific threads, unless there an urgent need to do something, well, platform specific.

Boost threads is portable and easy to use, but does have neither parallel patterns nor concurrent containers. You would need to manager threads manually, which can get ugly pretty quickly.

PThreads isn't available on Windows and its C. You really want to do multi-threading in C++, not C. RAII mixes well with mutexes and scoped locks.

Another option is PPL in Visual C++ 2010. It is similar to TBB, but as you may guess available for Windows only.

OpenMP is easy to use, but not very flexible. Since you already learned C++, you should use something more serious, such as TBB or PPL. For some strange reason Visual C++ 2010 doesn't support OpenMP 3. Too bad.

like image 45
pic11 Avatar answered Oct 07 '22 10:10

pic11