Questions tagged [thread-safety]

A piece of code is thread-safe if it only manipulates data structures in a way that allows consistent execution of this code by multiple threads. A code may be thread safe, conditionally safe (mutual exclusion required) or unsafe (can only be safely used by one thread).

Filter by
Sorted by
Tagged with
0 votes
2 answers
72 views

Can a multi-threaded Python app change the CWD?

Would the assert statement in this code raise an error under any Python 3.11+ interpreter? Can the current working directory be changed by another module without this code using any async features? ...
Carlos Gil's user avatar
0 votes
0 answers
57 views

I can't get to the bottom of what is causing an iOS layout engine crash

I am noticing a lot of logs with the following crash in Crashlytics: NSISSparseVectorAddTermWithPlaceValueCoefficientStartingIndex NSInternalInconsistencyException - Modifications to the layout ...
CristianMoisei's user avatar
0 votes
0 answers
32 views

Is it thread safe to call BackgroundJob.Enqueue one the same method/service in a loop?

I have an API that receives a large list of items that should be treated and inserted/updated in a database. the api serves a console app that runs as cron job. The list is within 10s of thousands of ...
hipsdog's user avatar
0 votes
1 answer
49 views

Is my actor based store really thread safe?

I'm trying to understand if my code is really thread safe. The read/write to storeData should be, but is the call to sync in didSet also thread safe? Also, is there a better practice to load initial ...
Yotam's user avatar
  • 9,873
-2 votes
1 answer
74 views

Collections.sort() Synchronized [closed]

I have a list and I need to sort it, in a thread-safe way, in Java. I need the return to be a list. I wrote the code below but since myListToOrder is already a synchronizedList I am not sure the ...
Catarina Nogueira's user avatar
0 votes
1 answer
50 views

C# WPF Splash Screen (Window) on Secondary Thread: Trying to understand why this works?

I have a C# WPF app. I have two windows (Main & Splash). Once started the processes of the main deserialzie various large chunks of data, language files, and saved dictionaries (on a custom ...
Gabriel's user avatar
  • 19
0 votes
0 answers
55 views

Can compiler optimize away locks accessible to only one thread?

C# provides reentrant locking: lock (obj) { ... } Can the current C# compiler deduce, from context, that only one thread will ever have access to obj and therefore optimize away all (reentrant) ...
Daniel Chin's user avatar
1 vote
1 answer
40 views

How can I update a cached list where other threads retain the old cache list

I have a cached list of users in my app. A couple of the admin pages need the list of all users (the total is small enough this isn't a problem). The approach I am taking is: When the cached list is ...
David Thielen's user avatar
0 votes
0 answers
28 views

How can OpenMP influence a non-OpenMP code?

I am working on debugging a code. Let me say up front that it is not my code and I do not have permission to share a single line of it. I know we all like to see code, but that's my situation. It ...
bob.sacamento's user avatar
0 votes
0 answers
66 views

Smart pointer usage when creating thread

Problem Description I have encountered compiling error when developing C++ program using std::thread(), I have solved this problem by myself, but I haven't found out the reason yet. Given the ...
SamuraiBUPT's user avatar
-1 votes
1 answer
31 views

Make test avoid relying on real time

I have two functions in my queue: void push(const T& object) { std::lock_guard<std::mutex> lock(mutex); queue.emplace(object); conditionVariable.notify_all(); } ...
Dewek's user avatar
  • 1
1 vote
0 answers
94 views

Extent of Thread Safety in R/ Rcpp

I know that R, and by extension Rcpp, are generally not thread-safe. This script e.g. crashes R: // [[Rcpp::plugins(openmp)]] #include <Rcpp.h> #include <omp.h> // [[Rcpp::export]] Rcpp::...
user's user avatar
  • 1,037
-3 votes
2 answers
58 views

Thread Safety in operations involving ConcurrentHashMap in Java

Context: I'm creating a new Item, caching it and returning it, however, its name must be unique. We're talking about multithreaded env. My questions are in the comments. class ItemOperations { ...
HyperVol's user avatar
  • 150
0 votes
0 answers
37 views

Java thread safety for long stream writer code

I'm making an intercom system where the speaker will be transmitting a continuous stream of bytes (audio/video) to multiple sockets depending on how many receivers are registered and which ones of ...
Allahjane's user avatar
  • 1,918
-1 votes
1 answer
63 views

How to know if a specific Java class is thread-safe?

Is it true that all classes not under java.util.concurrent are unsafe in a multi-threaded environment? If number 1 is wrong, how do you know if a specific class is thread-safe?
firefly_0's user avatar
0 votes
0 answers
21 views

Gson Type Adapter is not thread safe?

Here is my BigDecimal adapter (to serialize the amount based on configuration): public class BigDecimalAdapter extends TypeAdapter<BigDecimal> { @Override public void write(final ...
Chânhéng Seang's user avatar
-1 votes
0 answers
35 views

Unexpected value set in globals in drools

I am working on a Java application that uses the Drools rule engine to evaluate multiple DRL files, each containing different rules. Each rule is supposed to add a value to a global List if certain ...
learner's user avatar
  • 43
1 vote
2 answers
70 views

Do we need to create a copy of a variable inside a crtitical section?

void Animal::notifyEatingInitiated() { log.debug(TAG, "notifyEatingInitiated called"); std::lock_guard<std::mutex> lock(m_mutex); auto observers = m_observers; ...
Ashish Kumar Jha's user avatar
0 votes
1 answer
78 views

Thread safety and memory leak consideration on lookup collection with synchronization

In a program I am working on (an ASP.NET application) I have an in-memory lookup collection that is static readonly (shared among all thread of the ASP.NET application). I know this is not orthodox ...
Skary's user avatar
  • 1,322
-1 votes
1 answer
118 views

How to synchronise constructor with the rest of the class

Say I define a toy class that I want to claim is thread-safe. (Added following long discussion in comments: This a thought experiment about a race that I believe is commonly present and largely benign,...
jeremiah's user avatar
  • 115
3 votes
1 answer
130 views

Is TInterfacedObject autofree thread safe in Delphi?

Is TInterfacedObject._Release and TInterfacedObject._AddRef thread safe? I can see FRefCount is thread safe, but Destroy is not protected by locks or something else. function TInterfacedObject....
qgi's user avatar
  • 35
2 votes
0 answers
23 views

why we need to use const reference as arguments to thread functions

When I use "int &x" in Modify_X, then I get below error, but if I use const int &x, then the program works fine. What is the difference between these 2 scenarios. #include <...
Amruta Dhanawade's user avatar
3 votes
1 answer
88 views

Trying to implement a thread-safe cache

I'm trying to implement a shared-state, thread-safe cache, using a HashMap behind a RwLock. Ideally, I would like my interface to expose a single function, which -- given the key -- will return a ...
Xophmeister's user avatar
  • 8,894
1 vote
0 answers
64 views

How can I implement a thread-safe lazy-initialized singleton in C# without using the `Lazy<T>` class? [duplicate]

I've been reading about the Singleton pattern in C# and how thread safety can be a concern, especially when it comes to lazy initialization. I'm aware of the Lazy<T> class which can be used to ...
Fatih Furkan Çambel's user avatar
0 votes
1 answer
24 views

Sending data from and to sockets while using threads

I am currently in the process of programming Mensch Ärgere dich nicht in Java. But I'm getting nowhere, because the server that sends the game files and the client get the wrong data (both run in one ...
nojiyi4811's user avatar
0 votes
1 answer
47 views

Is synchronization needed when sharing a List<T> inside of a method in Parallel.ForEach?

I just saw following code snippet: public List<string> ProcessText(List<student> students) { List<student> result = new(); Parallel.ForEach(students, (st) => { ...
pmn's user avatar
  • 2,196
-1 votes
1 answer
94 views

Parallel.ForEach and SQL insert in batches of 20

I am writing some code inside a Parallel.ForEach statement. Each thread is supposed to do some work, then insert the result in a List. When the list hits a count of %20, it should insert the items ...
Rafa Ayadi's user avatar
0 votes
1 answer
29 views

Update progressbar from Task.Run generate deadlock

I am not experienced about WinForm so probably my understanding is wrong. But I think I have implemented all the syncronization needed to syncronize the worker thread with UI thread, despite that at ...
Skary's user avatar
  • 1,322
1 vote
0 answers
28 views

How to initialise a custom WKWebView view without having to explicitly trigger the main thread (@MainActor)

I have a custom WKWebView that I want to utilise in my code: public class AuthenticatedWebView: WKWebView, AdInAuthenticatedWebView { private var shouldStopFurtherRedirects = false public weak ...
Saad Qureshi's user avatar
0 votes
1 answer
65 views

Task<IEnumerable<T>> thread safety issues

I am creating all the variables within a task and I don't see any shared state. One of the MyData value (eg: myData.Chocolate) was null in one of the instances where I made a call. I didn't have logs ...
Punter Vicky's user avatar
1 vote
1 answer
45 views

how to tell Rust borrow checker that the thread will be joined before reference becomes dead [duplicate]

I'm writing a dummy program that uses a struct holding a string reference. I believe I specified the lifetimes correctly (at least, the compiler does not complain on the struct definition and method ...
los vatos locos's user avatar
1 vote
1 answer
18 views

How can I safely access a variable between Python processes which use ProcessPoolExecutor?

I have a script that uses multiprocessing to execute many io-bound tasks. I want to access a variable between processes in a safe manner. Is there a simple way to do this does not involve low level ...
scrollout's user avatar
  • 459
0 votes
0 answers
30 views

Is Redisson thread-safe itself?

Redisson client is said to be thread-safe. My question is, if Redisson and its object instances are thread-safe, what is the use of Locks like RLock? Does that enhance performance on multi-instance ...
PhoneMyint's user avatar
0 votes
1 answer
184 views

is there an equivalent of Python's multiprocessing module for Rust

In python there's a multiprocessing module where you can run a function inside a child process, I know what you're thinking, but Rust doesn't have Gil, wouldn't threads do the same thing for you? No, ...
Ali's user avatar
  • 448
0 votes
0 answers
43 views

How to resolve null pointer exception while using pubsub MessageReceiver

I had java scheduler based implementation to read gcp pubsub msg but then I changed the same to use gcp pubsub MessageReceiver to listen and read msg asynchronously. // Class that implemented ...
stackUser's user avatar
  • 565
2 votes
0 answers
66 views

Is overwriting Lists while another Thread is reading Thread-Safe?

I have a static List in my class, the content is loaded from a file whose content i get through an API endpoint. Along with the list i save a timestamp when the list was last refreshed or checked for ...
Patrick's user avatar
  • 33
0 votes
0 answers
18 views

Android graphics - how to synchronize the list of drawn objects for the onDraw() method

I'm trying to draw different objects on the canvas in my CustomView. The list of objects to draw is created dynamically outside the view and is passed as a parameter to the list of objects to draw. I ...
VJe's user avatar
  • 98
0 votes
1 answer
94 views

Python: RuntimeError: main thread is not in main loop (Threading & Tkinter)

I'm encountering this error when I run my code every time I understand it has something to do with threading but I'm a beginner so I do not understand much about it I tried searching on Google, ...
XIXX's user avatar
  • 11
0 votes
1 answer
37 views

Does DataFlow Object need to be locked if same object used in different thread

I am using the same dataflow objects in different threads. Do locks have to be used on the dataflow objects or are the objects automatically thread safe? For example: // Dataflow object defined ...
srf's user avatar
  • 21
-1 votes
1 answer
89 views

Using Numerous Atomic Variables, to eliminate mutex(s), has unexplained Consequences

After 5 days working on this (3 major rewrites), I seek help from you wise people on StackOverflow. I’ve used atomic int(s) before, but not this extensively. THE SCENARIO; I have batches, 1..1000+ ...
Adrian E's user avatar
0 votes
1 answer
26 views

How to implement thread counted singleton with constructor and destructor?

I have multiple threads that construct a parametrized object and call start and stop on it. The issue is, that the underlying service for each parameter should be only started on the first start, and ...
KamilCuk's user avatar
  • 122k
1 vote
1 answer
134 views

How should an MQTTnet client's lifecycle be managed?

tl;dr: How can I avoid disposing the MQTTnet client while it is in use on another thread? Perhaps this pertains to any IDisposable, but in the case of ManagedMqttClient, there are also calls like ...
Joe Schrag's user avatar
0 votes
0 answers
37 views

Application with multithreading stopping when trying to save data with Hibernate

I'm working on a personal project and I have multiple threads that calls the method save in the DAO, the first thread that executes the method works fine but the second one stops while executing the ...
Luiz Fernando da Silva Santos's user avatar
0 votes
2 answers
65 views

How to do change of static variables in a singleton in a threadsafe way?

I have a singleton class with multiple static variables and a refresh method to change their values. eg. Class Singleton { @Getter(AccessLevel.Protected) private static Immutable vara; @Getter(...
Siddharth's user avatar
1 vote
1 answer
229 views

How do I fix "cannot be sent between threads safely" when using tokio::spawn and Box<dyn Error>?

This simple program yields a compiler error: #[tokio::main] async fn main() { tokio::spawn(async { foo().await; }); } async fn foo() { let f1 = bar(); let f2 = bar(); ...
guenhter's user avatar
  • 11.3k
0 votes
0 answers
88 views

newSingleThreadContext() causes outofmemoryexeception in my service on Android

I am using *newSingleThreadContext()* to create a work and a arch threads. And I get outofmemory exception. enter image description here Here is the code : "sampleId" is called everytime we ...
Vishwas Kulkarni's user avatar
0 votes
0 answers
12 views

BaseJdbcLogger queryStack multi thread not safe

i have opend sql debug log by adding: logging.level.com.***.mapper=debug then two thread in one request execute query separately by: Mono.zip(selectA(session),selectB(session)).flatMap(insertC(session)...
hal hu's user avatar
  • 1
0 votes
0 answers
111 views

Safely share unique_ptr across threads

How do you safely share unique_ptr across threads? I need one thread to be able to reset the underlying object, and the other thread to be able to access it in a safe way. There is no atomic_exchange ...
Nicolò's user avatar
  • 19
1 vote
1 answer
22 views

Variable value not being updated with multiple threads

Hi I'm currently developing a web app whereby my flask backend deals with prediction models and my sveltekit frontend retrieves the predicted data from the backend. As my backend uses data that is ...
Linus Tan 's user avatar
0 votes
0 answers
7 views

Looking for examples of product demonstrating real life application of Deferred callbacks (thread safe) system

I am looking for examples of product(s) demonstrating real life application of thread-safe Deferred callbacks system at higher levels i.e. other than OS level IO handling or event handling. Any leads ...
Unknown User's user avatar

1
2 3 4 5
187