thread-safety

Thread-safe asynchronous code in C#

Hi all, I asked the question below couple of weeks ago. Now, when reviewing my question and all the answers, a very important detail jumped into my eyes: In my second code example, isn't DoTheCodeThatNeedsToRunAsynchronously() executed in the main (UI) thread? Doesn't the timer just wait a second and then post an event to the main threa...

Java concurrency question

Possible Duplicate: Thread safety in Java class I'm reading Java concurrency in Practice (good book so far), and I've come to an example that puzzles me. The authors state that this class is not threadsafe public class MutableInteger{ private int number; public int getInt(){return number;} public void setInt(int val...

Should I protect operations on primitive types with mutexes for being thread-safe in C++?

What is the best approach to achieve thread-safety for rather simple operations? Consider a pair of functions: void setVal(int val) { this->_val = val; } int getVal() { return this->_val; } Since even assignments of primitive types aren't guaranteed to be atomic, should I modify every getter and setter in the program in th...

Can IWindsorContainer be instantiated through a static method?

I'm still groping around a bit with Castle Windsor. At the moment all my pages which need an IWindsorContainer instantiate one themselves through a property: private IWindsorContainer WindsorContainer { get { if (_windsorContainer == null) { _windsorContainer = new WindsorContainer(new XmlInterpreter(Server...

extend Thread and calling non Thread methods...

My program represents a graph. There are villages with roads and gnomes that run along them. The gnomes are threaded. There is a country bank that is not. When a gnome travels along a road it'll pay the toll to the bank. When the roads break, the bank will give up money to repair it. Will I have problems if two gnomes try to pay the bank...

Is return atomic and should I use temporary in getter to be thread safe?

Is it necessary to use a temporary here to be thread-safe? int getVal() { this->_mutex.lock(); int result = this->_val; this->_mutex.unlock(); return result; } I'll give you disassembly of simple RAII test function int test() { RAIITest raii; //let's say it's a scoped lock return 3; } { 0...

Thread-confinement/swingworkers

I am not clear about thread confinement. In swing all the gui components must be updated through the EDT. SwingWorker is provided in Java6 for lengthy operations, and in the done method the gui components can be updated. My understanding was that the gui components in the done() method are updated in the EDT. Therefore there should be no...

Serial Task Executor; is this thread safe?

I have a class that I've created to allow asynchronous sequential execution of tasks, using the ThreadPool as the means of execution. The idea is that I'll have multiple instances running serial tasks in the background, but I don't want to have a separate dedicated Thread for each instance. What I'd like to check is whether this class ...

Is reading a double not thread-safe?

Update: I just stumbled upon this in Eric Lippert's answer to another question (he is quoting the spec): Reads and writes of other types, including long, ulong, double, and decimal, as well as user-defined types, are not guaranteed to be atomic. OK, so reading a double is not atomic. This means the value could get modified ...

Why doesn't this code demonstrate the non-atomicity of reads/writes?

Reading this question, I wanted to test if I could demonstrate the non-atomicity of reads and writes on a type for which the atomicity of such operations is not guaranteed. private static double _d; [STAThread] static void Main() { new Thread(KeepMutating).Start(); KeepReading(); } private static void KeepReading() { whil...

Is this thread safe?

I am writing an application for Android and am using worker threads to process certain information. Having read through my code I am now unsure if it is thread safe. I have written a simplified version of my code, I have omitted the Handler object used to communicate with the main thread and obviously the process itself. public class m...

Asynctask Error Handling

I am using AsyncTask to perform some background calculations but I am unable to find a correct way to handle exceptions. Currently I am using the following code: private class MyTask extends AsyncTask<String, Void, String> { private int e = 0; @Override protected String doInBackground(String... params) { try ...

COM calls from multiple threads

If I call the same COM function from multiple threads to an in proc COM Dll, how thread safe is that? Do all my objects in the COM DLL also need to be thread safe for this to work reliably? ...

Python: safe to read values from an object in a thread?

I have a Python/wxPython program where the GUI is the main thread and I use another thread to load data from a file. Sometimes the files are big and slow to load so I use a wxPulse dialog to indicate progress. As I load the file, I count the number of lines that have been read in the counting thread, and I display this count in the wxP...

Is it possible to *safely* return a TCHAR* from a function?

I've created a function that will convert all the event notification codes to strings. Pretty simple stuff really. I've got a bunch of consts like const _bstr_t DIRECTSHOW_MSG_EC_ACTIVATE("A video window is being activated or deactivated."); const _bstr_t DIRECTSHOW_MSG_EC_BUFFERING_DATA("The graph is buffering data, or has stopped bu...

How can I set a min value in .Net without using a lock?

I have multiple threads accessing variables. I know how to write spinlocks and use the Threading.Interlocked methods to increment etc. variables. However, I want to perform the equivalent of: a = Math.Min(a, b) or a = a | 10 ... but without using a critical section. Is this possible? I know the 2nd line is possible in assembler, but ...

java array thread safety

ArrayList in java is thread safe.and it is implemented using array. So, is the access to arrays in java thread safe??does the access to arrays needs to be synchronized?? ...

Thread Safe web apps - why does it matter?

Why does being thread safe matter in a web app? Pylons (Python web framework) uses a global application variable which is not thread safe. Does this matter? Is it only a problem if I intend on using multi-threading? Or, does it mean that one user might not have updated state if another user... I'm just confusing myself. What's so importa...

How do I use Perl's `Thread::Pool::Simple`?

I'm using Thread::Pool::Simple for multi-threading. I have a couple of questions which are quite general to multi-threading, I guess: Each of my threads might die if something unexpected hapens. This is totally accepted by me, since it means some of my assertion are wrong and I need to redesign the code. Currently, when any thread dies ...

One thread changes BOOL to YES, another thread doesn't see change

I have a property declared as the following: @property(assign) BOOL die; One thread continuously checks if it should die by looking to see if that variable has changed to YES. When that die is set to YES (by a user clicking a button), the other thread that is grinding away still sees it as NO. I've put careful traces through the code ...