SomeUser
SomeUser

Reputation: 2041

What is easiest way to create multithreaded applications with C/C++?

What is the easiest way to create multithreaded applications with C/C++?

Upvotes: 6

Views: 3708

Answers (15)

Void - Othman
Void - Othman

Reputation: 3481

Besides the ones already mentioned, ACE is another popular and widely deployed C++ framework that provides thread encapsulations across multiple platforms. It's style of C++ isn't as modern as Boost.Thread, for example, but it is quite mature.

Upvotes: 2

Jherico
Jherico

Reputation: 29240

It's been a while since I worked in C++ and I haven't seen the Boost threading support, but I found it very helpful to encapsulate semaphore services provided by the OS, usually either POSIX or Win32, in simple classes that would acquire locks, and release them in the destructors, making their use fairly simple.

void operateOnSharedResource(SharableResource & foo) {
    MutexLock lock(foo.getMutex());
    // do stuff to foo
    // implicit call to MutexLock dtor performs release 
}

Ultimately there are lots of simple tricks like this to ease thread programming and I'd be surprised if Boost didn't have something like this by now (EDIT: It does and it's documented in Lock Types).

Regardless, the main problem with writing multi-threaded code isn't going to be solved by any third party library, and that's understanding where your code can be parallelized usefully, and where shared resources are going to be touched and must be accounted for. Here's a few rules of thumb I use when writing multi-threaded code.

  • Try to minimize the number of shared resources
  • Try to encapsulate shared resources in class wrappers that make all operations atomic.
  • Make worker threads as simple as possible

Proper encapsulation really does wonders for writing safer multi-threaded code, because the fewer things you can see, the fewer things can have a race condition.

Upvotes: 3

Max Lybbert
Max Lybbert

Reputation: 20039

This depends entirely on what you're doing. If you can fit what you're doing into OpenMP then that is the way to go. Otherwise you may want to look at Intel's TBB. TBB offers several workflows which you should be able to fit into, but the library is dual licensed and you may not be able to accept either license. If both OpenMP and TBB are out, then you should consider your operating system's thread pools abilities.

At some point you may need to bite the bullet and use Boost.Thread. If so, you'll want to look at what makes multithreading in C++ hard (good to read even if you're not using C++0x: "It's not the threads themselves, it's the communication that causes problems. Mutable shared state introduces implicit communication," page 3).

Upvotes: 1

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

The easiest way is by avoiding/minimizing mutable shared state.

Once you have mutable shared state, you need to deal with locking which is where the bulk of the difficulty in writing multi-threaded programs exists.

Upvotes: 1

torus
torus

Reputation: 1049

Boost.Thread is relatively easier because it's portable, well-documented and has high-level API such as scoped_try_lock.

Upvotes: 2

James Jones
James Jones

Reputation: 8763

The C++0x specification includes threading facilities (one of my favorite new features). Soon it won't matter what OS you're compiling for! Just look how easy it is to create a new thread and join back to the creator thread:

#include <thread>
#include <iostream>

class SayHello
{
public:
    void operator()() const
    {
        std::cout<<"hello"<<std::endl;
    }
};

int main()
{
    std::thread t((SayHello()));
    t.join();
}

Visual Studio 2010 is implementing parts of C++0x but we're still waiting on the threading facilities.

Upvotes: 2

Kazoom
Kazoom

Reputation: 5849

Posix Thread is quite good, they also come with great documentation and tutorials.

Not as easy as java threads, but still quite good.

Upvotes: 1

StackedCrooked
StackedCrooked

Reputation: 35485

I'm not sure about the easiest, but IMO the most user-friendly threading library is the one included in the Poco C++ project. For a preview have a look at the Thread.h header file.

Upvotes: 2

Tobias Langner
Tobias Langner

Reputation: 10808

Just to mention it because it hasn't been mentioned: a compiler with OpenMP support (http://openmp.org/wp/)

Upvotes: 8

Alan
Alan

Reputation: 4933

There is no simple answer to this. It depends very heavily on what you hope to gain from multithreading, the platform/compiler, and which threading model you want to use. Every threading API has its pitfalls.

And just because no one has mentioned it so far, OpenMP is another option that is supported in many modern mainstream compilers and is intended to simplify the use of concurrency. http://openmp.org/wp/

Upvotes: 5

Dirk is no longer here
Dirk is no longer here

Reputation: 368251

Qt has a pretty threading support and documentation but as others have cautioned this is not for beginners. The doc link I gave itself points to a short reading list

Upvotes: 7

chrism1
chrism1

Reputation: 657

I would say with Qt. Qt Threads and Qt Concurrency are probably worth googling.

Upvotes: 1

vehomzzz
vehomzzz

Reputation: 44588

unfortunately there is no easy way. Couple of options: pthread on linux, win32 api threads on windows or boost::thread library

Upvotes: 14

Esteban K&#252;ber
Esteban K&#252;ber

Reputation: 36832

There is no easy way to create a multithreaded application in any language.

Upvotes: 14

Carl Norum
Carl Norum

Reputation: 224944

pthreads!

https://computing.llnl.gov/tutorials/pthreads/

Upvotes: 5

Related Questions