4,817 questions
-1
votes
1
answer
74
views
why "breakpoint at futex" and write() affect each other in GDB
When I use gdb to debug futex locks with output, the program gets stuck in a weird loop.
#include <mutex>
#include <iostream>
#include <thread>
#include <unistd.h>
volatile int ...
1
vote
0
answers
118
views
Is there a GCC/Clang attribute to mark which fields in a struct that a given mutex covers?
I'm thinking about how I would theoretically implement a multi threaded queue from first principles, using only glibc on POSIX.
Obviously, this is going to require a mutex (or some other locking/...
-1
votes
1
answer
160
views
Thread-safety mutable cross-references in Rust
I want to implement two different structures that have references to each other, can mutate each other, and are thread-safe.
I can introduce the following example:
pub struct Player {
pub x: f32,
...
1
vote
1
answer
92
views
Questions about spinlock mutex implementation via std::atomic_flag
I'm currently reading a book called C++ concurrency in action. There is an example of spinlock mutex impl:
class spinlock_mutex
{
std::atomic_flag flag;
public:
spinlock_mutex():
flag(...
2
votes
0
answers
100
views
Mutex failure in sample_test_webrtc when compiled with clang on Linux
The webrtc repository has an internal sample application, named sample_test_webrtc (the folder's name where it's implementation resides). I built it using gn and ninja and it works.
Then I took that ...
-1
votes
1
answer
327
views
How exactly does CDialog::Create() work in MFC? Does some of its calls require the use of a mutex?
myDlg = new Dlg(doc, myDlg , myDlgID);
myDlg -> Create(myDlgID, this);
myDlg -> ChangeFolder(directory);
In the ChangeFolder function I use things, which are normally created only in ...
0
votes
1
answer
71
views
What is wrong in my Blocking Queue implementation
// Thread-safe Queue
template<class T>
class BlockingQueue {
public:
BlockingQueue() {
isempty.lock();
}
void push(T obj) {
std::lock_guard<...
0
votes
0
answers
62
views
Troubleshooting Synchronization Issue in a Multi-threaded C Program for Supermarket Simulation
I'm working on a multi-threaded C program that simulates a supermarket with restocking and potentially customer behaviors (currently commented out). The program involves several threads representing ...
0
votes
1
answer
291
views
Persistent Data Race Warnings in C++ Parallel Program despite Mutex Usage
I'm encountering persistent data race warnings in my C++ parallel program despite using mutexes. I've implemented a solution using OpenMP and mutexes to synchronize access to shared data, but I ...
2
votes
1
answer
152
views
Does operating system thread scheduling factor in to the decision to use std::sync::Mutex vs tokio::sync::Mutex?
The tokio::sync::mutex documentation mentions the following:
Contrary to popular belief, it is ok and often preferred to use the ordinary Mutex from the standard library in asynchronous code.
This ...
0
votes
1
answer
226
views
Replace boost::timed_wait() with std in c++17
I have a condition variable std::condition_variable my_cond;
I want to be able to replace boost::timed_wait() with an std equivalent one.
If the previously mentioned condition variable was a boost one,...
1
vote
1
answer
152
views
Holding a mutex lock after all writes are finished
I have a question about thread safety and mutex locks in C++. I know that at the simplest level, a mutex lock should be held at any point where there could be simultaneous reading and writing to the ...
1
vote
1
answer
132
views
Using rvalue in the condition statement of `for` loop in C++
I am working on a programming task that takes in an n value and uses three threads to print zero, even and odd numbers.
For example, if n = 3, it will print 010203. I encountered a strange behavior ...
0
votes
0
answers
125
views
Can two separate threads perform a write to a std::atomic<> variable?
I am looking for some clarity on the necessity to lock a variable if std::atomic<> is used on it. Please consider the following code:
std::atomic<int> my_integer;
void Thread1()
{
if (...
-2
votes
2
answers
2k
views
In go, how to use mutex to lock and wait with go routines
I have code that is something like this:
var lock sync.Mutex
func DoSomething() {
lock.Lock()
go func() {
defer lock.Unlock()
// Code here
}()
}
func Wait() {
lock.Lock()
lock....
2
votes
1
answer
235
views
Why is it false that anything wrapped inside Mutex is thread safe?
It seems to me that if a data structure requires a lock for its contents to be accessed, then it doesn't matter what sort of data structure is inside. Even if the stuff on the inside is not thread ...
8
votes
1
answer
377
views
Race condition in Morris's algorithm
I implemented Morris's algorithm for a no-starve mutex from the Little Book of Semaphores (page 85) almost verbatim. About half the time, it terminates correctly, and the other half it freezes up mid-...
0
votes
0
answers
252
views
c++ std::scoped_lock Caller failing to hold lock 'lock' before calling function 'func'
In my project I'm returning a std::scoped lock to guard some resources.
A very simple example is this
// Type your code here, or load an example.
#include <mutex>
class A
{
public:
...
1
vote
0
answers
86
views
RX interrupt using mBed OS Serial throwing Mutex error at runtime
Running mBed OS on a B-L475E-IOT01A1 board but currently not using any threads or Mutexs. I borrowed an example test script to isolate the bit of code not working, shown below. This just reads and ...
2
votes
0
answers
34
views
Synchronizing strings between threads with mutexes
I'm doing an exercise for an exam, which asks me to create 2 processes (parent and child), each one must create N threads (N is provided by user) and a file (also the filename is provided by user). ...
1
vote
1
answer
69
views
Mutexes do not seem to lock the Resources
I want that whenever there is switch on the file, My go routine reads from the new file. and for that I am using Mutexes to do so. But the output does not seem to be as expected. In f1.txt I have ...
1
vote
0
answers
63
views
How can I run a detached thread that is waiting for a conditional variable in a loop?
I've got a very time critical simulator that is supposed to run as fast as humanly possible. I tried to increase the data throughput by distributing every data frame to multiple worker threads. To ...
0
votes
1
answer
134
views
How to use Windows.h Mutexes with two threads
I'm facing a problem using mutexes in two different threads.
I have two thread functions: a producer, which receives data from the user and writes it to a global variable, and a consumer, which ...
-1
votes
2
answers
278
views
Why do we need a separate <shared_mutex> header file? [closed]
Can we just put shared_mutex under the same header file, rather than a separate <shared_mutex> header file?
When I use shared_mutex, I thought I only need to #include <mutex>. It turns ...
-1
votes
1
answer
53
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
594
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
101
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 ...
3
votes
1
answer
1k
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
434
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
149
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
360
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
88
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
139
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. ...
0
votes
0
answers
17
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
423
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 ...
-7
votes
4
answers
761
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
217
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
195
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 ...
6
votes
1
answer
695
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 ...
1
vote
0
answers
473
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
83
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
42
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
77
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
70
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
184
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 ...
-1
votes
1
answer
89
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
117
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, ...
4
votes
1
answer
204
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
128
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
220
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 ...