n元树中的多线程

我试图在lock_it()函数的n元树中使用多线程。看到了所有的教程。但是我无法提出代码。我只能删除死锁,但我想最大化lock_it()函数的并行运行。

阅读此链接,了解lock_it()的作用。 https://www.geeksforgeeks.org/locking-and-unlocking-of-resources-in-the-form-of-n-ary-tree/

如何改进它?

#include <bits/stdc++.h>
#include <thread>
#include <mutex>

using namespace std;
class Node {
public:
    char key;
    bool locked;
    int cnt_locked_desc;
    Node* parent;
    vector<Node*> child;
};
Node* root = NULL;
mutex mtx;
class LockUnlock {
public:
Node* newNode(char node_key,Node* prt) {
        Node* tmp = new Node;
        (*tmp).key = node_key;
        (*tmp).locked = (*tmp).cnt_locked_desc = 0;
        (*tmp).parent = prt;
        return tmp;
    }
    bool lock_it(Node* node) {  //O(h)
        if ((*node).cnt_locked_desc > 0 || node == NULL)
            return 0;
        for (Node* curr = node; curr != NULL; curr = (*curr).parent)
            if ((*curr).locked)
                return 0;
        mtx.lock();
        for (Node* curr = node; curr != NULL; curr = (*curr).parent) {
            (*curr).cnt_locked_desc++;
        }
        mtx.unlock();
        (*node).locked = 1;
        return 1;
    }
};
iCMS 回答:n元树中的多线程

请考虑直接使用互斥锁来使用 def open_imap(): # Read the config file config2 = configparser.ConfigParser() config2.read(os.path.join('C:\\','IziPayMail.ini')) # Connect to the server hostname = config2.get('Mail','hostname') logging.info('Hostname = ' + hostname) connection = imaplib.IMAP4_SSL(hostname) logging.info('Conectado a ' + hostname) # Login to our account username = config2.get('Mail','username') password = config2.get('Mail','password') connection.login(username,password) logging.info('Logeado a ' + username) return connection 索引。

std::lock_guard or std::scoped_lock?

并发是一个很难的话题。如果您有兴趣了解更多信息,我真的很喜欢这本书:https://www.manning.com/books/c-plus-plus-concurrency-in-action-second-edition

本课程:https://www.udemy.com/course/modern-cpp-concurrency-in-depth/

我也强烈推荐这门课程:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-172-performance-engineering-of-software-systems-fall-2018/

本文链接:https://www.f2er.com/1542446.html

大家都在问