天道酬勤,学无止境

mutex

How can I protect a vector with a Mutex?

I am working on designing a C++ server that accepts multiple different interacting clients, and I use vectors to keep track of all of them individually. However, I realized that, because of so many threads running, there's a tiny chance a vector might be read and written to at the same time by two threads. Is there a quick and safe way to add a mutex or something to them so that it will wait until all the reads are done until another function adds to it? Not doing so can mess up the protocol and maybe even crash the server. I had an idea to create a global variable that would lock all reads to

2022-01-19 00:44:46    分类:问答    c++   windows   multithreading   vector   mutex

C++ Locking stream operators with mutex

I need to lock stdout in my logging application to prevent string interleaving in multi-thread applications logging to stdout. Can't figure out how to use move constructor or std::move or sth else to move unique_lock to another object. I created objects for setting configs and encapsulation and figured out how to lock stdout with static std::mutex to lock from these objects (called shards). Something like this works for me: l->log(1, "Test message 1"); While that is fine and could be implemented with templates and variable number of parameters I would like to approach more stream-like

2022-01-18 00:05:35    分类:问答    multithreading   c++11   logging   stdout   mutex

Copy or Move Constructor for a class with a member std::mutex (or other non-copyable object)?

class A { private: class B { private: std::mutex mu; A* parent = NULL; public: B(A* const parent_ptr): parent(parent_ptr) {} B(const A::B & b_copy) { /* I thought I needed code here */ } }; public: B b = B(this); //...to make this copy instruction work. // (Copy constructor is deleted, need to declare a new one?) }; I have a class B that is basically a thread-safe task queue. It contains a deque, a mutex, and a condition_variable. It facilitates a consumer/producer relationship between any two threads that are started by the class A. I have simplified the code as much as possible. The problem

2022-01-16 23:57:12    分类:问答    class   mutex   copy-constructor   member   move-constructor

C++ 创建原子函数(C++ Creating an atomic function)

问题 void foo ( Bar* bar , void(Bar::*qux)(void) ) { if ( bar ) { bar->qux(); } } 问题是: bar可以在另一个线程检查后删除。 我无法将互斥成员添加到Bar以锁定它。 因此我想知道,如果我可以告诉处理器原子地运行这个函数,我该怎么做? 我在谷歌上花了很多时间,但没有找到可以理解的手册...... PS Debian,gcc,不允许Boost ,允许 C++11。 回答1 您可能希望使用具有共享所有权语义的智能指针(例如shared_ptr 、 intrusive_ptr )来确保对象在您引用它时保持活动状态。 回答2 您想暂时共享对象的所有权,以防止另一个线程将其删除。 这是shared_ptr的工作,当我们不需要访问它时,使用weak_ptr允许删除: void foo ( std::weak_ptr<Bar> weak_bar , void(Bar::*qux)(void) ) { if (std::shared_ptr<Bar> bar = weak_bar.lock()) { // We now share ownership of the object - it won't be deleted bar->qux(); } // We have released ownership - it

2022-01-16 13:42:02    分类:技术分享    c++   multithreading   pointers   mutex   atomic

C++ Creating an atomic function

void foo ( Bar* bar , void(Bar::*qux)(void) ) { if ( bar ) { bar->qux(); } } The problem is: bar can be deleted after the check by another thread. I can not add a mutex member to Bar in order to lock it. Thus I wonder, if I can tell the processor to run this function atomically, and how would I do so? I've spent way to much time on Google, but found no understandable manuals... P.S. Debian, gcc , Boost NOT allowed, C++11 IS allowed.

2022-01-16 12:25:53    分类:问答    c++   multithreading   pointers   mutex   atomic

在 Python 中使用具有并发期货的互斥锁(Using mutexes with concurrent futures in Python)

问题 我有一些代码使用并发期货连接到许多远程主机来运行一些命令。 例如: def set_host_to(host, value): connection = connect_to(host) info = do_something_with(connection) do_some_action(connection, value) with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: for host, value in things_to_do: executor.submit(set_host_to, host, value) 我现在有一个要求,其中一些期货不能同时运行,但在获得上述info之前无法确定哪些期货。 正确的解决方案是某种互斥锁吗? 假设info (或其中的一部分)是一个字符串,并且具有相同字符串的 2 个期货不应同时运行。 我将如何进行编码? 回答1 这可以使用下面的代码通过锁字典来完成。 locks = defaultdict(threading.Lock) main_lock = threading.Lock() with main_lock: lock = locks[info] with lock: do_some_action(connection, value)

2022-01-16 09:52:14    分类:技术分享    python   mutex   concurrent.futures

向条件变量发出信号(pthreads)(Signalling a condition variable (pthreads))

问题 假设某个条件变量“cond”与互斥变量“mutex”相关联。 如果一个线程在调用pthread_cond_wait(&cond,&mutex)之后在cond上休眠,并且另一个已锁定mutex锁的线程已完成,那么该线程在调用pthread_mutex_unlock(&mutex) pthread_cond_signal(&cond)是否重要? 如果它调用pthread_cond_signal(&cond) ,它是否甚至需要解锁互斥锁,因为睡眠线程无论如何都会获取互斥锁? 编辑:根据 https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview,“调用 pthread_cond_signal() 后未能解锁互斥锁可能不允许匹配的 pthread_cond_wait() 例程完成(它将保持阻塞)。 " 我想那时,解锁,也许只是之后,是必需的。 回答1 调用pthread_cond_signal后,您应该始终解锁互斥锁。 以下是一些值得阅读的好问题/答案: 在不锁定互斥锁的情况下调用 pthread_cond_signal 它现在不会来找我,但我很确定有充分的理由(就竞争条件而言)你不想在发出信号之前解锁互斥锁。 回答2 如果保持互斥锁锁定,则被唤醒的线程无法获取互斥锁,因此将阻塞在pthread_cond

2022-01-15 23:52:38    分类:技术分享    c   pthreads   posix   mutex   condition-variable

Signalling a condition variable (pthreads)

Suppose some condition variable "cond" is associated with a mutex variable "mutex". If a thread is sleeping on cond after calling pthread_cond_wait(&cond,&mutex), and another thread that has mutex locked is finished, does it matter whether that thread calls pthread_cond_signal(&cond) before or after calling pthread_mutex_unlock(&mutex) ? Does it even need to unlock the mutex at all if it calls pthread_cond_signal(&cond), since the sleeping thread will acquire the mutex anyway? EDIT: According to https://computing.llnl.gov/tutorials/pthreads/#ConVarOverview, "Failing to unlock the mutex after

2022-01-15 19:22:36    分类:问答    c   pthreads   posix   mutex   condition-variable

编译器说数据不能在线程之间安全地共享,即使数据包装在互斥锁中(Compiler says that data cannot be shared between threads safely even though the data is wrapped within a Mutex)

问题 我正在使用具有传递给 HTTP 请求的State Rocket。 该结构包含一个Mutex<DatastoreInstance> ,它提供对 SQLite 数据库的访问权限,并使用互斥锁锁定以确保读写安全。 pub struct DatastoreInstance { conn: Connection, } 当DatastoreInstance结构看起来像这样时,只有一个 SQLite 连接一切正常,但我还想在这个结构中添加一个事务对象: pub struct DatastoreInstance { conn: Connection, events_transaction: Transaction, } 这没有编译,因为Transaction对象需要引用一个Connection对象,该对象应该具有它知道的生命周期。 我正在使用的 rusqlite 中的Connection和Transaction对象定义如下: pub struct Connection { db: RefCell<InnerConnection>, cache: StatementCache, path: Option<PathBuf>, } pub struct Transaction<'conn> { conn: &'conn Connection, drop_behavior: DropBehavior

2022-01-15 18:27:09    分类:技术分享    rust   thread-safety   mutex   rust-rocket

异常时解锁互斥锁(Unlock mutex on exception)

问题 mutex.lock(); try { foo(); // can throw exception } catch (...) { mutex.unlock(); throw; } mutex.unlock(); 为了保证解锁,我必须在 catch 块和正常情况下调用mutex.unlock() 。 有什么办法可以避免重复吗? 谢谢 回答1 您正在寻找的是像 std::lock_guard 这样的互斥体包装器: #include <mutex> std::mutex _mutex; void call_foo() { std::lock_guard<std::mutex> lock(_mutex); try { foo(); // can throw exception } catch (...) { // the mutex is unlocked here... throw; } // ... and here } 当lock超出范围时,它的析构函数解锁底层互斥锁_mutex 。 另请参阅std::unique_lock,此类提供了更多功能并可能增加更多开销。 在这种情况下,一个std::lock_guard就足够了。

2022-01-15 16:54:33    分类:技术分享    c++   exception   mutex