Questions tagged [multithreading]

For questions regarding multi-threading, the ability of a computer or a program to perform work concurrently or asynchronously by utilizing multiple concurrent streams of execution (generally referred to as threads).

Multi-threading is a common model to implement SM in multi-cored machines, where threads share memory through shared variables. In this model in parallel programming a processor can create multiple threads that will be executed in parallel. Each thread has its own stack, variables and ID, considered private state. For every thread there is a unique heap shared by all, then considered shared memory.

In order to allow a volume of work to be most effectively and safely divided into multiple concurrent streams of execution, a number of critical areas need to be addressed.

  • Scheduling: ensure worker tasks are able to progress independently and effectively (e.g. deadlock/livelock avoidance)
  • Publication: ensure data altered in one thread is only visible to others as expected
  • Synchronization: ensure critical regions are protected from multiple concurrent updates causing data loss/corruption.

The underlying tenet of concurrent processing is Amdahl's law (graph). This law governs the diminishing amount of throughput that can be achieved by greater numbers of concurrent processors/cores. Therefore the overall aim of multi-threading is to minimize the amount of serial execution (exclusive locking) within any concurrent system.

Frequently Asked Questions

Books

More information:

Related tags

139166 questions
33
votes
5 answers

How can I set processor affinity to a thread or a Task in .NET?

Can we set two threads or two tasks to execute with different processor affinity in a C# application? I have read about SetThreadAffinityMask, but have found no example of how that should be used. Alternatively, is there any way for TPL (Task…
mita
  • 331
  • 1
  • 3
  • 3
33
votes
4 answers

Set CPU affinity when create a thread

I want to create a C++11 thread which I want it to run on my first core. I find that pthread_setaffinity_np and sched_setaffinity can change the CPU affinity of a thread and migrate it to the specified CPU. However this affinity specification…
Peng Zhang
  • 3,475
  • 4
  • 33
  • 41
33
votes
4 answers

Thread-safe cache libraries for .NET

Background: I maintain several Winforms apps and class libraries that either could or already do benefit from caching. I'm also aware of the Caching Application Block and the System.Web.Caching namespace (which, from what I've gathered, is…
Aaronaught
  • 120,909
  • 25
  • 266
  • 342
33
votes
8 answers

Difference between lock(locker) and lock(variable_which_I_am_using)

I'm using C# & .NEt 3.5. What is the difference between the OptionA and OptionB ? class MyClass { private object m_Locker = new object(); private Dicionary m_Hash = new Dictionary(); public void…
Matt
  • 41,216
  • 30
  • 109
  • 147
33
votes
2 answers

java.lang.RuntimeException: Only one Looper may be created per thread

I have a simple thread that goes like this: public class AwesomeRunnable extends Thread { Handler thisHandler = null; Handler uihandler = null; String update = null; long time = 0; public AwesomeRunnable(Handler h, long…
user3081519
  • 2,659
  • 5
  • 25
  • 35
33
votes
3 answers

ObservableCollection and threading

I have an ObservableCollection in my class. And further into my class I have a thread. From this thread I would like to add to my ObservableCollection. But I can't do this: This type of CollectionView does not support changes to its…
ErikTJ
  • 2,001
  • 3
  • 21
  • 38
33
votes
5 answers

Thread safe instantiation of a singleton

Which one synchronization method to use to ensure a singleton remains a singleton? +(Foo*)sharedInstance { @synchronized(self) { if (nil == _sharedInstance) { _sharedInstance = [[Foo alloc] init]; ... } …
33
votes
2 answers

Process.waitFor(), threads, and InputStreams

In pseudocode, here's what I'm doing: Process proc = runtime.exec(command); processOutputStreamInThread(proc.getInputStream()); processOutputStreamInThread(proc.getErrorStream()); proc.waitFor() However, sometimes processOutputStreamInThread…
Kaleb Pederson
  • 45,767
  • 19
  • 102
  • 147
33
votes
4 answers

Android AsyncTask context behavior

I've been working with AsyncTasks in Android and I am dealing with an issue. Take a simple example, an Activity with one AsyncTask. The task on the background does not do anything spectacular, it just sleeps for 8 seconds. At the end of the…
dnkoutso
  • 6,041
  • 4
  • 37
  • 58
33
votes
5 answers

How to start a thread after specified time delay in java

I have called a method in ServletContextListener as thread ..Now as per my need i have to delay the thread for 1 minutes and then start executing the method called in the thread but i am not able to do that as i am very new in this... Here is my…
Adi
  • 1,395
  • 11
  • 37
  • 61
33
votes
7 answers

Running two threads at the same time

I want to know if a program can run two threads at the same time (that is basically what it is used for correct?). But if I were to do a system call in one function where it runs on thread A, and have some other tasks running in another function…
user2644819
  • 1,787
  • 7
  • 28
  • 41
33
votes
7 answers

Android "Only the original thread that created a view hierarchy can touch its views." error in Fragment

I've got this simple timer in my app which is runs in every 3 seconds. It works perfectly if it's not in a fragment class. But here in fragment I always got the error: Only the original thread that created a view hierarchy can touch its…
David
  • 2,331
  • 4
  • 29
  • 42
33
votes
8 answers

Is ConcurrentHashMap.get() guaranteed to see a previous ConcurrentHashMap.put() by different thread?

Is ConcurrentHashMap.get() guaranteed to see a previous ConcurrentHashMap.put() by different thread? My expectation is that is is, and reading the JavaDocs seems to indicate so, but I am 99% convinced that reality is different. On my production…
Stu Thompson
  • 38,370
  • 19
  • 110
  • 156
33
votes
2 answers

What's the best way to exit a Haskell program?

I've got a program which uses several threads. As I understand it, when thread 0 exits, the entire program exits, regardless of any other threads which might still be running. The thing is, these other threads may have files open. Naturally, this is…
MathematicalOrchid
  • 61,854
  • 19
  • 123
  • 220
33
votes
6 answers

Atomic Operations and multithreading

Recently I was reading a tutorial, in that I came across a statement that says.. "The Java language specification guarantees that reading or writing a variable is an atomic operation(unless the variable is of type long or double). Operations…
MaheshVarma
  • 2,081
  • 7
  • 35
  • 58