multithreading

A ThreadStateException occures when trying to restart a thread

From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread. The code in question is as follows: // Make sure the thread is done stopping while (this.mThread.ThreadState == ThreadState.Running) { Thread.Sleep(0); } // Respawn a thread if the current one is stopped or doesn't exist if (this.mThread...

Interlocked.Exchange, but not for booleans?

Is there an equivalent for Interlocked.Exchange for boolean? i.e., an atomic exchange of values that returns the previous value and doesn't require locks? ...

Erlang-style Concurrency for Other Languages

What libraries exist for other programming languages to provide an Erlang-style concurrency model (processes, mailboxes, pattern-matching receive, etc.)? Note: I am specifically interested in things that are intended to be similar to Erlang, not just any threading or queueing library. ...

Sharepoint COMException 0x81020037

I am working on a SharePoint application that supports importing multiple documents in a single operation. I also have an ItemAdded event handler that performs some basic maintenance of the item metadata. This event fires for both imported documents and manually created ones. The final piece of the puzzle is a batch operation feature ...

Cleanest Way to Invoke Cross-Thread Events

I find that the .NET event model is such that I'll often be raising an event on one thread and listening for it on another thread. I was wondering what the cleanest way to marshal an event from a background thread onto my UI thread is. Based on the community suggestions, I've used this: // earlier in the code mCoolObject.CoolEvent+= ...

How to abort threads created with ThreadPool.QueueUserWorkItem

Hi, is there a way to abort threads created with QueueUserWorkItem? Or maybe I don't need to? What happens if the main application exits? Are all thread created from it aborted automatically? ...

Which is a better approach in logging - files or DB?

Okay, here's the scenario. I have a utility that processes tons of records, and enters information to the Database accordingly. It works on these records in multi-threaded batches. Each such batch writes to the same log file for creating a workflow trace for each record. Potentially, we could be making close to a million log writes in a...

Thread not waking up from Thread.Sleep()

We have a Windows Service written in C#. The service spawns a thread that does this: private void ThreadWorkerFunction() { while(false == _stop) // stop flag set by other thread { try { openConnection(); doStuff(); closeConnection(); } catch (Exception ex) { log.Error("Something went wr...

JavaScript and Threads

Is there some way to do multi-threading in JavaScript? ...

How do threads work in Python, and what are common Python-threading specific pitfalls?

I've been trying to wrap my head around how threads work in Python, and it's hard to find good information on how they operate. I may just be missing a link or something, but it seems like the official documentation isn't very thorough on the subject, and I haven't been able to find a good write-up. From what I can tell, only one thread...

How scalable is System.Threading.Timer?

I'm writing an app that will need to make use of Timers, but potentially very many of them. How scalable is the System.Threading.Timer class? The documentation merely say it's "lightweight", but doesn't explain further. Do these timers get sucked into a single thread (or very small threadpool) that processes all the callbacks on behal...

How do you minimize the number of threads used in a tcp server application?

I am looking for any strategies people use when implementing server applications that service client TCP (or UDP) requests: design patterns, implementation techniques, best practices, etc. Let's assume for the purposes of this question that the requests are relatively long-lived (several minutes) and that the traffic is time sensitive, ...

How do I spawn threads on different CPU cores?

Let's say I had a program in C# that did something computationally expensive, like encoding a list of WAV files into MP3's. Ordinarily I would encode the files one at a time, but let's say I wanted the program to figure out how many CPU cores I had and spin up an encoding thread on each core. So, when I run the program on a quad core CPU...

Are Python threads buggy?

A reliable coder friend told me that Python's current multi-threading implementation is seriously buggy - enough to avoid using altogether. What can said about this rumor? ...

C++ Thread question - setting a value to indicate the thread has finished

Is the following safe? I am new to threading and I want to delegate a time consuming process to a separate thread in my C++ program. Using the boost libraries I have written code something like this: thrd = new boost::thread(boost::bind(&myclass::mymethod, this, &finished_flag); Where finished_flag is a boolean member of my class. Whe...

What is a race condition?

When writing multi-threaded applications, one of the most common problems experienced are race conditions. My question to the community, is: What is a race condition? How do you detect them? How do you handle them? And finally, how do you prevent them from occurring? ...

What is a deadlock?

When writing multi-threaded applications, one of the most common problems experienced are deadlocks. My question to the community, is: What is a deadlock? How do you detect them? Do you handle them? And finally, how do you prevent them from occurring? ...

What is a semaphore?

A semaphore is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a semaphore and how do you use it? ...

What is a mutex?

A mutex is a programming concept that is frequently used to solve multi-threading problems. My question to the community: What is a mutex and how do you use it? ...

Java: notify() vs. notifyAll() all over again

If one google for "difference between notify() and notifyAll()" then a lot of explanations will pop up (leaving apart the javadoc paragraphs). It all boils down to the number of waiting threads being waken up: one in notify() and all in notifyAll(). However (if I do understand the difference between these methods right), only one thread...