简体   繁体   English

在多线程程序中共享资源C ++与Java?

[英]Sharing resources in Multi threaded programs C++ vs Java?

Hi I'm looking at the differences of how C++ and Java protect data corruption when sharing resources between multiple threads, in Java we can do many things like using the synchronized keyword: 嗨,我正在研究C ++和Java在多个线程之间共享资源时如何防止数据损坏的差异,在Java中,我们可以做很多事情,例如使用synced关键字:

public synchronized void inCounter
{
   this.counter++;
}

In C++ we can use a shared pointer: 在C ++中,我们可以使用共享指针:

shared_ptr<Song> sp7(nullptr);

My main question is the main differences I have to look at when using C++ and sharing resources and whether or not we can use synchronization the same as Java, coming from a Java background I am trying to learn more about C++. 我的主要问题是,在使用C ++和共享资源时,我必须查看主要区别,以及我们是否可以使用与Java相同的同步,这是从Java背景开始的,我试图学习有关C ++的更多信息。

Looking at the differences is of limited value imho. 观察差异是有限的,恕我直言。 You really need to ignore what Java does and study up on how to do multithreading in C++ . 您确实需要忽略Java做什么,并研究如何在C++执行多线程。

It is a long time since I used Java but I seem to remember its synchronization is fairly intuitive and high level. 自从我使用Java已经很长时间了,但是我似乎记得它的同步是相当直观和高级的。 In C++ you can use various techniques at different levels giving a great deal more flexibility and opportunity to be more efficient. C++您可以在不同的级别使用各种技术,从而提供更大的灵活性和提高效率的机会。

Here is a rough guide to how C++ can be used to implement higher level synchronization, similar to that of (what I remember of) Java . 这是有关如何使用C++来实现更高级别的同步的粗略指南,类似于Java (我记得)。 But remember there is much more to multithreading and shared resources than this. 但是请记住,多线程和共享资源的功能远不止于此。

#include <mutex>

class MyClass
{
    std::recursive_mutex class_mtx; // class level synchronization

    std::vector<std::string> vec;
    std::mutex vec_mtx; // specific resource synchronization

public:

    void /* synchronized */ func_1()
    {
        std::lock_guard<std::recursive_mutex> lock(class_mtx);

        // everything here is class level synchronized

        // only one thread at a time here or in func_2 or in other
        // blocks locked to class_mtx
    }

    void /* synchronized */ func_2()
    {
        std::lock_guard<std::recursive_mutex> lock(class_mtx);

        // everything here is class level synchronized

        // only one thread at a time here or in func_1 or in other
        // blocks locked to class_mtx
    }

    void func_3()
    {
        /* synchronized(this) */
        {
            std::lock_guard<std::recursive_mutex> lock(class_mtx);

            // everything here is class level synchronized
            // along with func_1 and func_2
        }

        // but not here
    }

    void func_4()
    {
        // do stuff

        /* sychronized(vec) */
        {
            std::lock_guard<std::mutex> lock(vec_mtx);

            // only the vector is protected

            // vector is locked until end of this block
        }

        // vector is not locked here
    }

    void func_5()
    {
        static std::mutex mtx; // single function synchronization

        std::lock_guard<std::mutex> lock(mtx);

        // this function is synchronized independent of other functions
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM