Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how threads communicate with eachother?

Tags:

java

c#

.net-3.5

how threads communicate with eachother? they dont use values of eachother, then what is the way of communication between them?

like image 729
vivek Avatar asked Aug 13 '10 15:08

vivek


People also ask

How do threads communicate between them?

Cooperation (Inter-thread communication) is a mechanism in which a thread is paused running in its critical section and another thread is allowed to enter (or lock) in the same critical section to be executed.It is implemented by following methods of Object class: wait() notify() notifyAll()

Can threads of different process communicate with each other?

Processes are typically independent of each other. Threads exist as the subset of a process. Threads can communicate with each other more easily than processes can.

How do threads share data with one another?

All static and controlled data is shared between threads. All other data can also be shared through arguments/parameters and through based references, as long as the data is allocated and is not freed until all of the threads have finished using the data.

How do two threads communicate with each other in C?

producer and consumer threads should capture each other's tid. producer on producing can send: pthread_kill(consumerID, SIGUSR1); consumer is setup with the signal handler for SIGUSR1, and can retrieve the produced result from the common std::queue saved by pthread_setspecific().


1 Answers

There are a few ways threads can communicate with each other. This list is not exhaustive, but does include the most used strategies.

  • Shared memory, like a variable or some other data structure
  • Synchronization primitives, like locks and sempahores
  • Events, like ManualResetEvent or AutoResetEvent

Shared memory

public static void Main()
{
  string text = "Hello World";
  var thread = new Thread(
    () =>
    {
      Console.WriteLine(text); // variable read by worker thread
    });
  thread.Start();
  Console.WriteLine(text); // variable read by main thread
}

Synchronization primitives

public static void Main()
{
  var lockObj = new Object();
  int x = 0;
  var thread = new Thread(
    () =>
    {
      while (true)
      {
        lock (lockObj) // blocks until main thread releases the lock
        {
          x++;
        }
      }
    });
  thread.Start();
  while (true)
  {
    lock (lockObj) // blocks until worker thread releases the lock
    {
      x++;
      Console.WriteLine(x);
    }
  }
}

Events

public static void Main()
{
  var are = new AutoResetEvent(false);
  var thread = new Thread(
    () =>
    {
      while (true)
      {
        Thread.Sleep(1000);
        are.Set(); // worker thread signals the event
      }
    });
  thread.Start();
  while (are.WaitOne()) // main thread waits for the event to be signaled
  {
    Console.WriteLine(DateTime.Now);
  }
}
like image 101
Brian Gideon Avatar answered Nov 05 '22 05:11

Brian Gideon