0

I have two process write and read. I want to complete write process asynchronously using 10 threads and after all thread complete writing. I want to start 5 threads for Reading Process. How to check writing all threads finished its task so that I can signal main thread to start reading process asynchronously by using 5 threads.

    public static void MainThread()
    {

        new Thread(() => WriterThread(10)).Start();
        a_ResetEvent.WaitOne();
        new Thread(() => ReadWorkerThread(5)).Start();
        

    }

    public static void WriterThread(int _noThread)
    {
        Thread t = null; 


        for (int i = 0; i < _noThread; i++)
        {
          t =  new Thread(() => Write());
          t.Start();
        }

        if(t.ThreadState != ThreadState.Running)
        a_ResetEvent.Set();
    }

    public static void ReadWorkerThread(int _noThread)
    {
        for (int i = 0; i < _noThread; i++)
        {
            new Thread(() => Read()).Start();
        }
    }


    public static void Write()
    {
        Console.WriteLine($"Writing Thread- {Thread.CurrentThread.ManagedThreadId}");
        Thread.Sleep(5000);
        Console.WriteLine($"Writing Completed Thread -{Thread.CurrentThread.ManagedThreadId}");
        
    }

    public static void Read()
    {
        Console.WriteLine($"Reading Thread - {Thread.CurrentThread.ManagedThreadId}");
        Thread.Sleep(1000);
        Console.WriteLine($"Reading Completed Thread - {Thread.CurrentThread.ManagedThreadId}");
    } 
MiralShah
  • 19
  • 11

1 Answers1

0

The simplest option would be to use Parallel.For loops.

But without more information it is difficult to tell if this is a good idea or not. IO operations typically do not benefit much from parallelism, and running things in parallel requires your program to be thread safe, and that may make your program more complicated, or risks introducing bugs.

JonasH
  • 28,608
  • 2
  • 10
  • 23
  • Question post is just idea and actual problem is I am converting tiff files (1 millions files) to PDF files because of 1 millions files program throw memory exception so I have categorized files in bunch of 1000 files executed them one by one. Let me know your valuable feedback. – MiralShah Feb 07 '22 at 15:58