Questions tagged [mutex]
A mutex ("mutual exclusion") is a mechanism to ensure integrity when the same data or resource is accessed (in particular, changed) concurrently from several threads.
4,683
questions
-1
votes
2
answers
194
views
Why do we need a separate <shared_mutex> header file? [closed]
Can we just put shared_mutex under same header file, rather than separate <shared_mutex> header file?
When I use shared_mutex, I think I only need to #include <mutex>. It turns out I need ...
-1
votes
1
answer
44
views
How to serve the SQL data prepare only once -- requested independently from more processes (mutex in SQL)?
The goal: The usp_prepare_data SQL Server stored procedure should check whether the requested data is prepared or not. If the data is ready to be used, the usp_prepare_data should return quickly. If ...
2
votes
1
answer
56
views
How to move struct variable with Mutex data in Rust?
I'm trying to translate a C++ program that uses a Bank object, with methods that change an account's balance and uses mutexes so this can happen in parallel. In this program, each account had a lock I ...
0
votes
0
answers
51
views
c++ concurrent getline on ifstream
I want to perform a join on three files and as a last step, I have to compute a sum from a certain column of the joined file.
Let's say this is the part of the code where I read the last file line by ...
0
votes
0
answers
149
views
Relation of Mutex and CPU caches (and memory fences)
Suppose I have an application with multiple threads that need to access some shared data.
I know that a mutex (Critical Section) can be used to ensure that at most one thread at a time can access the ...
0
votes
0
answers
12
views
LiteDB throws unhandled exception when trying to open a shared connection
I am developing on .net Framework 4.8 with LiteDb 5.0.17.0.
When I try to instantiate a database connection with:
Connection = new LiteDatabase("Filename=C:\\temp\\data.ldb;Connection=Shared"...
-1
votes
1
answer
82
views
Is it legal to lock the std::mutex in main thread and then unlock in the child thread? [duplicate]
Is it legal to lock the std::mutex in main thread and then unlock in the child thread.
Here is a demo snippet, which seems work.
#include <iostream>
#include <mutex>
#include <thread>...
1
vote
1
answer
53
views
How to lock multiple critical sections in code?
I'm learning about multi-threading in C# and found out that the Mutex class, can help me to synchronize threads working. So, I want to use the ThreadPool (limited to 10 threads) and this thread's ...
-2
votes
2
answers
84
views
is it possible that `g_count++` is executed before init work is finished?
is it possible that g_count++ is executed before init work is finished? ( I mean, maybe the complier or cpu would change their execution order )
#include <iostream>
#include <mutex>
#...
0
votes
1
answer
57
views
Conflict with pthreads and mutex lock
I'm trying to do the following assignment:
You will write a program that creates three threads. These threads access a shared integer, buffer, one at a time. The buffer will initially be set to 0. ...
-1
votes
0
answers
43
views
Unexpected C26117 using scoped_lock
For some reason I'm getting the Intellisense warning C26117 using scoped_lock in one of my functions.
And I don't understand why
bool CastleManager::loopCastleBuffs(CPlayer* player, std::function<...
0
votes
0
answers
10
views
C - possible reordering issue with pipe access and mutex
I have 2 threads running after write_fd was initialized by a call to pipe():
static bool finished = false;
Thread 1
pthread_mutex_lock(&lock);
if (!finished) {
write(write_fd, buf, buf_len);
}...
0
votes
1
answer
70
views
How to synchronize this special case of threads with C++ std::thread
I need some help for following case: I have a main program that starts multiple instances of the same function as threads. The function that gets called is split in at least two parts. First, every ...
-1
votes
0
answers
35
views
How to receive information from thread to main thread and then give information back / C language, pthreads, mutex
The number of threads should be specified by #define NUM_THREADS number
Program version using mutex or semaphore
Write a program in which each thread generates two random numbers.
Then the main ...
-6
votes
4
answers
207
views
Passing lock ownership
With RAII, we have std::unique_lock and std::scoped_lock. Those two are explicitly movable. RAII objects are "locked" when constructed and "unlocked" when destructed. So, I see ...
0
votes
2
answers
87
views
Problems with locking single producer, multiple consumer fifo queue
I have this little simple queue where a one task read from a file into the queue and several tasks unpack the content. I works for a while, but eventually crashes because the queue is empty even ...
2
votes
0
answers
184
views
Locking safety of datastructure [closed]
I have a data structure with:
Multiple items 'A'.
Each A has multiple items B. Each B is linked to exactly 1 A.
Each B has multiple items C. Each C is linked to exactly 1 B.
For a local search ...
5
votes
1
answer
127
views
Does condition variable notify_one keep trying until it reaches thread awaiting with a positive predicate?
I'm testing edge cases with std::condition_variable and I tested scenario to starve one thread.
Scenario is that there are 99 producers and only one consumer, all of them working on 1 queue with max ...
0
votes
0
answers
64
views
producer and consumer problem with c++20 semaphore
Here is producer and consumer problem implementation with c++20 semaphore adapted from wikipedia https://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem. Do we still need std::lock_guardstd::...
1
vote
0
answers
27
views
Rust: static objects or something like Java Spring Component
I am building an application using Rust + Iced. I have various services for creating and manipulating my data. As of now I have one struct that contains all of the services and I pass it around as ...
0
votes
0
answers
41
views
Not smoothy in drawing when using multithread in c++
My program has this function:
1 thread for vehicle moving and 1 thread for user_input and 1 thread for using that input to move the people.
And after 1 step, I will draw the vehicle and people. If I ...
0
votes
1
answer
50
views
Sometimes my binary semaphore does not wait the correct time
Sometimes it does not wait long enough. I probably missed something simple - but I cant find it. Why from time to time wait function is returning prematurely
#define SEMAPHORE_MAXWAIT -1
#define ...
1
vote
1
answer
63
views
How could I synchronize multiple threads with only the use of pthread mutexes?
I was trying to make three threads that each open a different input file, read a single character from it and set it to a global variable and then wait for the next thread to read a single character ...
0
votes
1
answer
77
views
What is the proper way to use conditional variables?
My assignment provides running code that uses a high percentage of the CPU when running. The goal is to reduce that amount by implementing conditional variables in the producer consumer problem.
I ...
0
votes
0
answers
16
views
In CPython what happen to a mutex when its owning thread is "killed"?
As the title stated, I have a question regarding threads and mutex in CPython.
So my program use multiple threads and one of those threads is a "controller" which is responsible
of ...
0
votes
0
answers
66
views
TanStack query / React query with mutex, refreshtoken, autorization
Using TanStack query/React query with mutex. To update the refresh token for 401 status and for more than 2 requests. I couldn't connect with async-mutex or async-lock via interceptors axios. Could ...
0
votes
0
answers
14
views
two unique_lock a same mutex performs differently with/without compile option -pthread
Source code in main.cpp:
#include <iostream>
#include <mutex>
#include <thread>
int main()
{
std::mutex mtx;
std::cout << "Start" << std::endl;
...
0
votes
1
answer
45
views
Synchronization of 2 Mutex in 2 methods
I have 2 methods TIC and TAC which just output "TIC" or "TAC". And my goal is after creating multiple threads of TIC and TAC.
Final result should be:
TIC TAC TIC TAC TIC TAC
Here i ...
-2
votes
1
answer
71
views
Use object guarded by Mutex
I want to implement a lazy static reqwest::ClientBuilder. NB this is for a utilities module, so I have a feeling it'd be difficult to avoid using a static in this case. I want to construct it once, ...
0
votes
0
answers
19
views
named eventwaithandle autoreset even not work in Task.Run but works with thread
In c# I create use a EventWaitHandle to create a named autorest event the is initially not signaled.
If the EventWaithandle instance is used thread method handler called by using task.Run the WaitOne ...
0
votes
0
answers
25
views
Reboots on SoC mt7621a and kernel 5.0.19-rt10
I'm currently working on the development of a product with se MT7621A SoC and with kernel 5.0.19.rt10.
I'm having rebooting problems and in order to debug it, I activated some kernel options as Detect ...
4
votes
1
answer
154
views
Why standard doesn't require std::mutex::~mutex synchronizes-with with the latest unlock
struct X
{
std::mutex m;
std::string str;
void set(std::string s)
{
auto _ = std::unique_lock(m);
str = std::move(s);
}
~X()
{
// auto _ =...
3
votes
2
answers
84
views
How would you implement a thread-safe function which reads from a shared hashtable using a key and updates the value in a multi-threaded environment?
Suppose we have some function which takes in a key, retreives its value from a shared hashtable, and perform some operations on it to obtain a new value , and then updates the hashtable with this new ...
0
votes
1
answer
64
views
acquiring nested rwlocks in a thread-safe way?
I am managing some per-customer data in a thread-safe way as follows:
customer_id_to_data: Arc<RwLock<HashMap<CustomerId, Arc<RwLock<CustomerData>>>>>
It's very important ...
0
votes
0
answers
58
views
how to implement mutexes in a callback function which is called by multiple threads at the same time
Am trying to create a timer functionality using Windows timers, which will be called by multiple threads running concurrently. The callback function modifies a global array. I have used mutexes but am ...
0
votes
0
answers
27
views
WebView2 can't load client certificate
folks, I am writing a c# WPF program using WebView2 to access a inner website which requires mutual SSL authentication. I holds a valid client certificate. But I am stuck in how to let webview2 ...
1
vote
2
answers
93
views
Could std::atomic<std::shared_ptr<Struct Demo>> could be safely used with C++11 since the said partial specializations is only available with C++20?
As per the std::atomic article on cppreference, which says that (emphasis mine):
Defined in header <atomic>
template< class T >
struct atomic; // (since C++11)
template< class U >
...
0
votes
1
answer
26
views
How to lock siblings to prevent race condition
Models:
Class Checklist
has_many :checklist_items
end
Class ChecklistItem
belongs_to :checklist
after_save :update_checklist_completion
def update_checklist_completion
if siblings....
0
votes
1
answer
63
views
std::lock_guard does not lock the mutex as expected [duplicate]
In the following example, func should hang because it attempts to get a lock after main:
#include <cstdio>
#include <mutex>
std::mutex myMutex;
void func(void)
{
std::lock_guard<...
0
votes
0
answers
52
views
Issue in establishing a connection between the server and client-side programs in an Inter-Process Communication practice program using Named Pipes
I have developed a Windows Forms application using C# with the goal of implementing Inter-Process Communication (IPC) between two separate solutions. Each solution has a component for sending and ...
0
votes
1
answer
54
views
ReentrantMutexGuard borrow_mut getting blocked
Can somebody explain why my code panics when borrow_mut() is executed on a ReentrantMutexGuard. This is my code
use std::sync::Mutex;
use std::cell::RefCell;
use parking_lot::ReentrantMutex;
#[...
1
vote
1
answer
205
views
How to use a managed Tauri state variable inside a spawned tauri async runtime thread?
I am trying to build this app where I have a command that starts a thread that publishes many messages to RabbitMQ, and then emits an event once it is done publishing.
I have a managed ConnectionMutex ...
0
votes
1
answer
41
views
deadlock, when different fields of a mutex guarded struct are accessed accross multiple methods
How to avoid deadlock when different fields of a mutex guarded struct are accessed across multiple functions like below
use std::sync::Mutex;
#[derive(Default)]
struct InnerData {
ia: i32,
ib:...
0
votes
0
answers
40
views
Why does a locked mutex prevent http.ResponseWriter from sending a response?
I understand that one should unlock a mutex using defer in case an error comes up. Otherwise, if you left the mutex locked, other goroutines would be blocked if they tried to access it. But what I don'...
0
votes
2
answers
73
views
C# Mutex not beeing released? EZCAD2MUTEX_MARKING | Check if EzCad is Marking
I'm trying to detect whether the EzCad2 software is currently laser marking (mutex locked/signaled) or idle (mutex released/nonsignaled) using a publicly documented mutex. According to the ...
0
votes
1
answer
74
views
make model_averaging exclusive in federated learning using Python Threads
I am creating num_of_clients threads using the following code:
sockets_thread = []
no_of_client = 1
all_data = b""
while True:
try:
for i in range(no_of_client):
...
0
votes
1
answer
54
views
C++ settings return value mutex
In my application running on an ESP32 using FreeRTOS, I've implemented a setting class responsible for storing various settings. I'm currently concerned about the thread safety of this class, ...
0
votes
2
answers
74
views
How to mutex on a static function (in Java)?
I've been struggling with java mutexing for a while now, I'm trying to have ByteIterator objects each print a new (and unique) byte[] they get from the JobQueue, but they continually print repeat ...
0
votes
0
answers
43
views
I can't figure out why do I have a data-race
I am solving the dining philosophers problem to learn threads in C.
From what I understood about data races, I just have to lock a mutex, change it's value, then unlock.
Looks like this method works ...
0
votes
0
answers
76
views
Why my mutexes doesn't grant exclusive access?
My C program has three threads A, B, and C that share some buffers. Each buffer is protected by a mutex, and each thread has to lock the mutex before writing/reading a buffer.
A writes the buffers, ...