multithreading

Multi core CPU single thread behaviour, not achieving 100%

As you can see from the attached image the CPU graph on my dual core machine is weirdly symmetrical! Is this some sort of load balancing to prevent one core being used more than the other? What are the reasons behind it (heat distribution maybe)? Of course my main concern: is my single thread PSNR image algorithm achieving 100%? CPU...

Keep U.I Updated using Threads

Hello, I have a lot of long-running activities and think that spawning this activity off to another thread will be a good way to have my U.I be able to update to show its current status. However, when I use the following: Thread t = new Thread(() => { /* do magic here */ }); Nothing inside the foreach loop that's inside the threa...

Running Python-script in thread and redirecting std.out/std.err to wx.TextCtrl in GUI.

I'm trying to write a GUI that reads in settings for a python-script, then generates the script and runs it. The script can take dozens of minutes to run so in order to not block the GUI and frustrate the user I'm running it in a separate thread. Before I did this I used a separate class to redirect the std.out and std.err of the program...

Managing multiple-processes: What are the common strategies?

While multithreading is faster in some cases, sometimes we just want to spawn multiple worker processes to do work. This has the benefits of not crashing the main app if one of the worker crashes, and that the user doesn't need to worry a lot about inter-locking stuffs. COM+'s Application Pooling seems like a good way to achieve this on...

Why we need Thread.MemoryBarrier()?

In C#4 in a Nutshell, the author show that this class can write 0 sometimes without MemoryBarrier. I can´t reproduce in my Core2Duo: public class Foo { int _answer; bool _complete; public void A() { _answer = 123; //Thread.MemoryBarrier(); // Barrier 1 _complete = true; //Thread.MemoryB...

Is there a way to check if a NSLock was acquired?

Hi, I am working on multithreaded code. Data access is locked in several sections via "NSLock" objects. I want to ensure that some methods which are called within these sections check if their appropriate lock was aquired. Something like: assert([myLock isSet] == YES); I can't find something like "isSet" in NSLock. Any ideas how to ...

Does Func<T>.BeginInvoke use the ThreadPool?

When you call the BeginInvoke method on a Func delegates (or the Action delegates for that matter) in C#, does the runtime use the ThreadPool or spawn a new thread? I'm almost certain that it'll use the ThreadPool as that'd be the logical thing to do but would appreciate it if someone could confirm this. Thanks, ...

Free a TThread either automatically or manually

Hello all! I have a main thread and a separate thread in my program. If the separate thread finishes before the main thread, it should free itself automatically. If the main thread finishes first, it should free the separate thread. I know about FreeOnTerminate, and I've read that you have to be careful using it. My question is, is th...

How to close a blocked socket

A windows service has an open socket that is receiving data (on a separate thread) In response to the service OnShutdown I would like to signal worker thread to shutdown, but it's currently blocked on the Receive. At the moment I'm timing out on the Receive to check if there is a stop request pending. Is there a better approach, rathe...

How to insulate a job/thread from crashes

I'm working on a library where I'm farming various tasks out to some third-party libraries that do some relatively sketchy or dangerous platform-specific work. (In specific, I'm writing a mathematical function parser that calls JIT-compilers, like LLVM or libjit, to build machine code.) In practice, these third-party libraries have a t...

Background consumer thread lifetime management best practices

I have a C# class library which starts up a background consumer thread (lazily) which listens for tasks to complete from a producer/consumer queue. This class library can be used from any type of .NET application and is currently in use under an ASP.NET MVC web site. The consumer thread is blocked most of the time until a request comes i...

How do I determine the appropriate check interval?

I'm just starting to work on a tornado application that is having some CPU issues. The CPU time will monotonically grow as time goes by, maxing out the CPU at 100%. The system is currently designed to not block the main thread. If it needs to do something that blocks and asynchronous drivers aren't available, it will spawn another thr...

[Java] Sleeping till NEXT Second

I am finding often the need to very often wait until the next second to do the next operation in a series. This slows down unit tests quite considerably. So here's my question: Instead of doing Thread.sleep(1000) is there a quicker more efficient way to sleep until the second changes to the next second? Say the time is 1:00:89 I slee...

How would I loop through a collection in a separate thread and reflect the current item on the bound ListBox?

I have a ListBox bound to an ObservableCollection. The items in the collection each signify a different step with a method to perform that step. What I would like to do is have a BackgroundWorker loop through the collection, run each item's action method, and have the current step be reflected by the ListBox. My first iteration used n...

php multithreading problem

I am writing a php cron job that reads thousands of feeds / web pages using curl and stores the content in a database. How do I restrict the number of threads to, lets say, 6? i.e., even though I need to scan thousands of feeds / web pages, I want only 6 curl threads active at any time so that my server and network don't get bogged down....

Array of Sockets

I had a quick question on how i would maintain a connection of 1000 sockets. Im using threads to initiate writing and reading of the streams. However when I pass the socket to the new thread, the thread won't exit after excecution. I wanted to know if passing the socket would keep the thread on because it was still connected. ...

Python Threading socket

Hi Alls, I'm trying to implement a threading functionality for this answer : http://stackoverflow.com/questions/3491727/scanning-a-class-c-network-python/3492399#3492399 So far i have something like this: ...[snip].. m = re.search("/", str(host)) if m : net,_,mask = host.partition('/') mask = int(mask) net = atod(net) for...

Question on using Monitor.TryEnter and locking object

Consider the following function that implements non-blocking access to only the one thread. public bool TryCancelGroup() { if (Monitor.TryEnter(_locked)) { if (_locked == false) { _locked = true; try { // do something } catch (Exception ...

C# static field locking

Do I need locking for _userByNameQuery static field Yes/No and way? public class SomeClass { static Func<Entities, string, IQueryable<User>> _userByNameQuery = CompiledQuery.Compile<Entities, string, IQueryable<User>> ((context, userName) => context.Users.Where(u => u.UserName.ToUpper() == userN...

LINQ to SQL DAL, is this thread safe?

My code is written with C# and the data layer is using LINQ to SQL that fill/load detached object classes. I have recently changed the code to work with multi threads and i'm pretty sure my DAL isn't thread safe. Can you tell me if PopCall() and Count() are thread safe and if not how do i fix them? public class DAL { //read one Ca...