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

前端 未结 15 1551
遥遥无期
遥遥无期 2021-02-06 11:05

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

相关标签:
15条回答
  • 2021-02-06 11:14

    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.

    0 讨论(0)
  • 2021-02-06 11:17

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

    0 讨论(0)
  • 2021-02-06 11:18

    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.

    0 讨论(0)
  • 2021-02-06 11:18

    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.

    0 讨论(0)
  • 2021-02-06 11:20

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

    0 讨论(0)
  • 2021-02-06 11:25

    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.

    0 讨论(0)
提交回复
热议问题