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,355
questions
0
votes
1
answer
13
views
Producer Consumer Never terminating
I am using multi thread and I have made a program where producer creates values and insert into buffer and consumer consumes it.
However consumer never terminates. I would like to terminate the ...
0
votes
2
answers
31
views
Can you share mutexes using a common struct between pthreads in C?
I've been working on a multithreading exercise in C using POSIX threads that implements a thread pool design. My strategy was to embed the required and shared concurrency primitives in a struct ...
0
votes
0
answers
35
views
Is it dangerous/bad to call pthread_mutex_destroy with an uninitialized mutex
Example code:
#include <pthread.h>
int main(void)
{
pthread_mutex_t mutex;
pthread_mutex_destroy(&mutex);
return (0);
}
Is this a problem? I want to use a function that frees ...
0
votes
1
answer
23
views
C program hanging when executing thread in producer-consumer problem
I'm reproducing the producer-consumer in C using semaphores from semaphore.h and threads and mutexes from pthread.h with a very simple implementation. However, when I compile and run my program I won'...
-2
votes
0
answers
36
views
How to get field values of a struct passed to a function as a pointer [closed]
I have the following function definition.
func createSandBoxRoute(prodRoute *routev3.Route, apiLevelBasePathSand string) *routev3.Route {
route := routev3.Route{
Name: ...
0
votes
1
answer
28
views
Why and how is mutex working without initialising it using pthread_mutex_init()?
I am new to pthreads. I tried out a basic code from https://www.youtube.com/watch?v=xoXzp4B8aQk&list=PLfqABt5AS4FmuQf70psXrsMLEDQXNkLq2&index=5. I forgot to initialise the mutex using ...
0
votes
0
answers
52
views
Should a mutex be used in this context?
Let's assume the following conditions. I have two threads that are both working on the same class. Both have access to the member variables of the class. However, none of the threads share reads/...
0
votes
1
answer
60
views
Why do I need multiple mutexes?
I am currently looking at a code example below (also can be found here).
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
std::mutex m_a, m_b, m_c;
int ...
-2
votes
0
answers
58
views
Thread safety of return statement
I have some doubts about thread safety. Suppose multiple threads want to call this function
first
bool empty() {
return this->size == 0;
}
In above case, it is easy to understand that locking ...
0
votes
0
answers
28
views
Wait for mutex or custom timeout/clock
We got a special situation in our embedded system.
We need to wait for OS-objects (semaphore, mq, mutex, sockets) and kick the waiters out after a user specified time.
All these objects have wait ...
0
votes
1
answer
54
views
How to use condition appropriately in Java?
My two Threads, Produce and Consume, don't work well. When I run this code, the console printed 'producing' and 'consuming' one by one. Then, it stopped and program is still running>
class ...
0
votes
0
answers
15
views
What's a better way to implement a semaphore or mutex lock mechanism? [duplicate]
I'm new to OS, and I read a question on textbook today.
Instead of doing this:
bool AquireLock(int mutex)
{
while (mutex > 0); // do nothing until mutex == 0
mutex++;
return true;
}
...
3
votes
1
answer
51
views
Does std::mutex enforce cache cohesion?
I have a non-atomic variable my_var and an std::mutex my_mut. I assume up to this point in the code, the programmer has followed this rule:
Each time the programmer modifies or writes to my_var, he ...
0
votes
0
answers
9
views
Mutex timeout in javscript does'nt show on what line in code the error occurs
I use Node v16.14.2 and run this program in the command prompt.
I have a little problem with using Mutex in javascript. The Mutex itself do work but sometimes it takes to long to aquire the Mutex and ...
1
vote
0
answers
18
views
Node / Express race condition on DB operations
I have an express route that needs to retrieve a row from a db, check if a field has a value set and if not set it. The row data then gets sent back to the client.
I know that node runs on a single ...
0
votes
0
answers
54
views
I am parsing RoboCopy logs from Millions of files, How can I make my code run Faster?
New to StackOverflow, I'll do my best to post correctly :)
Hoping someone can help me to get my code running faster.
The code is run against RoboCopy Migration logs from a massive DFS server migration ...
0
votes
1
answer
46
views
How to use a Mutex to share writable memory? cannot move out of dereference of std::sync::MutexGuard [duplicate]
Code:
pub async fn start_orderbook_updater(websocket: Arc<Mutex<Ws>>) {
let mut ws = *websocket.lock().unwrap();
Error:
error[E0507]: cannot move out of dereference of `std::sync::...
0
votes
2
answers
50
views
Python threads stuck
I'm trying to switch from one thread called CameraProcessing to another thread called ServerKeypoints and viceversa.
To be precise, CameraProcessing changes a global variable called VALUE while ...
-1
votes
1
answer
55
views
Creating a pool of locks in C#
I would like to use exactly one of a set of resources from a multi-threaded C# application. These resources are not thread-safe, so some lock or mutex must be for them. How should I do it?
I would ...
1
vote
1
answer
84
views
How to acquire pointer to associated mutex
I'm working with mutex and when building the code, its throwing an error: 'error: use of deleted function ‘std::mutex::mutex(const std::mutex&)'
I understand why I'm getting this error, referred ...
0
votes
3
answers
73
views
Why doesn't mutex work without lock guard?
I have the following code:
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>
int shared_var {0};
std::mutex shared_mutex;
void task_1()
{
while (...
1
vote
1
answer
49
views
When does std priority queue compare the values?
I have a priority queue and the compare function references a value accessed by multiple threads. So it has to be protected by a mutex. Except I don't know when this compare function is ran. Is it ran ...
0
votes
0
answers
34
views
Sleep while holding mutex
My question relates to
What is disadvantage of calling sleep() inside mutex lock?
I have a similar situation, and I am not quite understanding why sleeping while holding a mutex is such a no-no. What ...
0
votes
0
answers
39
views
C: How to guard static variables in multithreaded environment?
Suppose having the following code elements working on a fifo buffer:
static uint_fast32_t buffer_start;
static uint_fast32_t buffer_end;
static mutex_t buffer_guard;
(...)
void buffer_write(uint8_t* ...
-2
votes
1
answer
73
views
Philosophers 42 lock order issue [closed]
I tested my program with Valdgrind --tool=helgrind and I have 2 warning regarding "Thread #5: lock order "0x4A82168 before 0x4A82108" violated"
This is below the original message:
=...
-1
votes
1
answer
47
views
Get the fastest answer from multiple thread call
I’m working on remote user management with radius, ldap and stuff in c++, I wanted to make the best use of the multiple servers that someone would configure in our program and take the fastest answer ...
-1
votes
0
answers
41
views
the performance difference between atomic.AddInt64 and sync.Mutex?
I'm learning about golang sync/atomic and did some performance test. below is the test code.
var TestNum int64
var benchCount = 1000000
func Benchmark_T1(b *testing.B) {
wg := sync.WaitGroup{}
...
2
votes
1
answer
43
views
Is it possible to capture return value of function used in std::call_once?
Suppose I have a function returning an expensive object and I want it to call exactly once while having an access to return value of the function.
Is this achievable with std::once_flag and std::...
1
vote
0
answers
42
views
program that starts two threads and waits for their completion
My program contains two threads for which they are expected to end. I use a single variable that is shared between threads and this is x and a single mutex.
The q key ends the thread
Problems I want ...
-1
votes
2
answers
72
views
C# WPF, Multi-Threads for Queue
Thread1 does Enqueue()
Thread2, 3 does Dequeue()
Thread 2, 3 has same mutex, when i used different mutex, sometime dequeue works twice.
When i use same mutex in Thread 1,2,3 it works fine. What is ...
0
votes
1
answer
54
views
Why does the spin check while(condition){} not avoid the std::condition_variable::wait(lock, condition)?
I try to build a fair lock among threads. Each thread runs a for loop to do ++iCounter. Each thread can do ++iCounter one time, then issue cv.notify_one() to wake up another thread using cv.wait(ul, ...
0
votes
1
answer
18
views
best way to use multiple mutex one for each structure ? (2 structures being manipulated by 4 threads for example)
I am working for the first time on a "large" scale with threads in C and i am uncertain about how to properly use multiple mutex with each mutex protecting a structure and the structure ...
0
votes
2
answers
155
views
How bad it is to lock a mutex in an infinite loop or an update function
std::queue<double> some_q;
std::mutex mu_q;
/* an update function may be an event observer */
void UpdateFunc()
{
/* some other processing */
std::lock_guard lock{ mu_q };
while (!...
0
votes
0
answers
27
views
Mutex in WRK lua script
I have the following script in Lua (custom.lua):
local data = loadFile()
local count = -1
request = function()
url_path = "/resource"
-- lock count
count += 1
local ...
0
votes
1
answer
131
views
destroy std::mutex vs pthread_mutex_t
What is the difference between std::mutex and pthread_mutex_t in CPP?
What is the correct way to destroy each one of the mutex types when they are dynamically allocated as follows:
pthread_mutex_t * ...
0
votes
0
answers
42
views
With MinGW g++ 9.2.0 I get an error: 'mutex' is not a member of 'std'
I updated my g++ because my older version didn't fully support std::filesystem, but now I cannot use mutexes at all. Example code:
#include <mutex>
int main(const int argc, const char** argv)
{
...
0
votes
0
answers
11
views
how to emulate a reverse/inverse counting mutex using only binary, counting, and recursive mutexes?
I'm not sure if reverse/inverse counting mutex is the name of the synchronization primitive I'm looking for, and search results are vague. What I need is the ability for a thread that wants to write ...
0
votes
0
answers
26
views
robust_list not calling FUTEX_WAKE
The Linux robust_list mechanism is a tool used by robust mutexes to support automatic unlocking in the event that the lock owner fails to unlock before terminating, maybe due to unexpected death. ...
0
votes
0
answers
16
views
Why mutex is used in reader writer problem in os
Reader writer problem uses one mutex and one binary semaphore.Binary semaphore and mutex have almost same working. Then why mutex is used to lock read count, rather than using binary semaphore. Why 2 ...
0
votes
0
answers
33
views
receiving nonsensical error from pthread_create
I'm trying to implement a concurrent queue in C. When running it, the line pthread_create(&p2, NULL, (void*)queue_enqueue, p2_args); gives this error: ../nptl/pthread_mutex_lock.c:81: ...
1
vote
2
answers
61
views
Problems with map, goroutine and mutex
This project is made to receive POST routes that will finally count as access to later write to a database. The intuition is to save interaction with the database of another project in production. I ...
2
votes
0
answers
127
views
How can I update my DB with a new version without interference with clients (golang / sqlite3)?
The database I am using is a global variable, initialized reading the file sql-repo.db:
const dbFile = "sql-repo.db"
var globalDB *LocalDB
type LocalDB struct {
Path string
...
1
vote
0
answers
40
views
Notify std::condition_variable that waits on the std::future returned by std::async from within the async function
Given the following code my question is whether precondition of the std::condition_variable is guaranteed to see the std::future in the state after the lambda returned, i.e. std::future_status::ready. ...
0
votes
0
answers
40
views
Conditionally and efficiently locking on array of mutexes in C++
I have a simple work that is parallelized with OpenMP with very low scaling (2x speedup with 8 cores) due to too many locking:
struct MutexWithoutFalseSharing
{
std::mutex mut;
char padding[(...
0
votes
0
answers
26
views
Cross-talk of condition_variables signal and how to avoid it
I'm using the mutex and condition_variable pair to implement mult-threaded processing. I have read examples and solid explanations like this and that. However, I do not understand why separate ...
1
vote
1
answer
65
views
Emulating C#'s attribute functionality in C++
I am trying to write an interface for a reader/writer lock in C++ using std::shared_mutex, and I want it to automatically recognize which functions need to use std::unique_lock (writers) and which ...
1
vote
2
answers
60
views
how to write copy constructor in thread safe class c++
template<typename T>
class TSQueue {
public:
TSQueue() {}
TSQueue(const TSQueue& rhs) {
lock_guard<mutex> lg1(rhs._mutex);
_data = rhs._data;
}
private:
...
0
votes
0
answers
20
views
is the overhead of PTHREAD_MUTEX_ERRORCHECK mutex type significant over PTHREAD_MUTEX_NORMAL types?
Am trying to understand if the overhead caused by mutex checks for mutex attribute type PTHREAD_MUTEX_ERRORCHECK is significant over the PTHREAD_MUTEX_NORMAL type. I mean, are there any benchmark ...
0
votes
0
answers
36
views
Converting a pthread_cond_t and/or a pthread_mutex_t to a ctype python object
I need to solve a synchronization problem between processes by utilizing a shared memory that will contain a conditional variable and a mutex. The problem is that the shared memory is being parsed ...
0
votes
0
answers
42
views
Sharing variables between threads in C++ on Windows
I need to communicate with robot via sockets (this part works) and I'd like to make a watchdog to monitor if the robot is ready or not.
The communication handling is in while loop until the robot ends ...