Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to do effective multithreading

How to do effective multithreading

Scheduled Pinned Locked Moved C#
tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cyn8
    wrote on last edited by
    #1

    hi, i'm new to multithreading and i found it abit hard to understand. For example, i i were to have 3 thread which each prints the letter 'A','B' and 'C' respectively for 3 times(meaning, in total there should be 3 As,3 Bs and 3 Cs) and it is suppose to output as such ABCAABBCC. But how can i do it? I thought of using Join method to ensure that the thread which writes A will execute the final 2 As and the same for Bs and Cs.....However, it does not seem to work... what should i take note of to ensure that it works accordingly? Below are the codes that i've written wrongly i suppose. using System; using System.Threading; namespace MultiThreading2 { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Thread t1=new Thread(new ThreadStart(WriteA)); Thread t2=new Thread(new ThreadStart(WriteB)); Thread t3=new Thread(new ThreadStart(WriteC)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.ReadLine(); } static void WriteA() { for(int i=0;i<3;i++) {Console.WriteLine("A"); Thread.Sleep(2000); } } static void WriteB() {for(int j=0;j<3;j++) { Console.WriteLine("B"); Thread.Sleep(2000); } } static void WriteC() {for(int k=0;k<3;k++) { Console.WriteLine("C"); Thread.Sleep(2000); } } } } Thanks for any replies:)

    L 1 Reply Last reply
    0
    • C cyn8

      hi, i'm new to multithreading and i found it abit hard to understand. For example, i i were to have 3 thread which each prints the letter 'A','B' and 'C' respectively for 3 times(meaning, in total there should be 3 As,3 Bs and 3 Cs) and it is suppose to output as such ABCAABBCC. But how can i do it? I thought of using Join method to ensure that the thread which writes A will execute the final 2 As and the same for Bs and Cs.....However, it does not seem to work... what should i take note of to ensure that it works accordingly? Below are the codes that i've written wrongly i suppose. using System; using System.Threading; namespace MultiThreading2 { /// /// Summary description for Class1. /// class Class1 { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { Thread t1=new Thread(new ThreadStart(WriteA)); Thread t2=new Thread(new ThreadStart(WriteB)); Thread t3=new Thread(new ThreadStart(WriteC)); t1.Start(); t2.Start(); t3.Start(); t1.Join(); t2.Join(); t3.Join(); Console.ReadLine(); } static void WriteA() { for(int i=0;i<3;i++) {Console.WriteLine("A"); Thread.Sleep(2000); } } static void WriteB() {for(int j=0;j<3;j++) { Console.WriteLine("B"); Thread.Sleep(2000); } } static void WriteC() {for(int k=0;k<3;k++) { Console.WriteLine("C"); Thread.Sleep(2000); } } } } Thanks for any replies:)

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, the fundamental idea of multithreading is to perform multiple tasks in a rather asynchronous fashion, i.e. without predetermined order of execution; the goal is to make optimal use of available resources e.g. performing calculations on data as soon as such data is available. If the subjobs must be executed in a very specific order, multithreading simply makes no sense, since now the logic makes everything fully predictable. Examples of unpredictable execution could include: - some long winding calculations; - some user interaction; - a background printjob; - a database query; all of the above combined in one app. It would not make much sense to orchestrate this in a fixed manner since one never knows which subjob will take what time to execute, hence asyncrhonous execution, using several threads. In a typical application, there may well be a need to synchronize two or more threads from time to time. Several mechanisms are available for this, typically one thread waits for something (say a consumer needing some data), and another thread signals something (say a producer providing some data). Known mechanisms include: AutoResetEvent, ManualResetEvent, Queues, Semaphores, ... Joining two threads is a drastic synchronization since now one of the threads no longer runs. I would suggest you: - search and read some articles on multi-threading, here on CodeProject; - experiment with a more appropriate example. Hope this helps.

      Luc Pattyn [Forum Guidelines] [My Articles]


      this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups