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. Multithreading issue

Multithreading issue

Scheduled Pinned Locked Moved C#
helpquestioncomcareer
5 Posts 3 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.
  • M Offline
    M Offline
    maria_p
    wrote on last edited by
    #1

    Hi Folks, I wonder if somebody can either point me in the right direction or explain a certain concept to me... i just can't figure it out (i've browssed through MSDN)...If in an application I have several threads running and while these threads are running something arrives on the serial port of the computer...Using a control I can raise an event to signal this "BUT" will the application be aware that this event has taken place? Or how can I make sure that the application is aware that something has been recieved at the serial port while other threads are running?.... Please can someone help... A secondary issue also related ... say again multiple threads are running in an application, in one thread I implement a lock on a certain portion of code (so this must complete execution before any other thread can resume control)... if a timer raises an event (or is due to raise an event) while another thread is executing a portion of locked code... will this timer event be lost by the time the locked code in the other thread completes??? can anyone help!!! I'm desperate... can't find this information anywhere... Maria (phillips_maria@hotmail.com)

    C H 2 Replies Last reply
    0
    • M maria_p

      Hi Folks, I wonder if somebody can either point me in the right direction or explain a certain concept to me... i just can't figure it out (i've browssed through MSDN)...If in an application I have several threads running and while these threads are running something arrives on the serial port of the computer...Using a control I can raise an event to signal this "BUT" will the application be aware that this event has taken place? Or how can I make sure that the application is aware that something has been recieved at the serial port while other threads are running?.... Please can someone help... A secondary issue also related ... say again multiple threads are running in an application, in one thread I implement a lock on a certain portion of code (so this must complete execution before any other thread can resume control)... if a timer raises an event (or is due to raise an event) while another thread is executing a portion of locked code... will this timer event be lost by the time the locked code in the other thread completes??? can anyone help!!! I'm desperate... can't find this information anywhere... Maria (phillips_maria@hotmail.com)

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      maria_p wrote: say again multiple threads are running in an application, in one thread I implement a lock on a certain portion of code (so this must complete execution before any other thread can resume control)... Now, what I remember from my concurrency lectures (8 to 10 years ago) if you lock a piece of code (the "critical section") other code in other threads can still execute as they are not preventing from executing, they just can't execute the locked code. Think of the critical section as the centre of a highway intersection with an American 4-way Stop sign. (In America the rule is the first to arrive has right of way and can pass through the intersection). So the first car arrives and is allowed onto the intersection. A fraction of a second later another car arrives from a different direction, it must wait until the first car has passed through. This is locking. Now, just because some traffic has to wait to gain access to this intersection doesn't stop the traffic flowing on an interstate a mile away. Does this help you understand how concurrency (multithreading) works? --Colin Mackay--

      EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

      M H 2 Replies Last reply
      0
      • C Colin Angus Mackay

        maria_p wrote: say again multiple threads are running in an application, in one thread I implement a lock on a certain portion of code (so this must complete execution before any other thread can resume control)... Now, what I remember from my concurrency lectures (8 to 10 years ago) if you lock a piece of code (the "critical section") other code in other threads can still execute as they are not preventing from executing, they just can't execute the locked code. Think of the critical section as the centre of a highway intersection with an American 4-way Stop sign. (In America the rule is the first to arrive has right of way and can pass through the intersection). So the first car arrives and is allowed onto the intersection. A fraction of a second later another car arrives from a different direction, it must wait until the first car has passed through. This is locking. Now, just because some traffic has to wait to gain access to this intersection doesn't stop the traffic flowing on an interstate a mile away. Does this help you understand how concurrency (multithreading) works? --Colin Mackay--

        EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

        M Offline
        M Offline
        maria_p
        wrote on last edited by
        #3

        Yes thank you Sir, what you have said and the way you put it has given me a totally different way of looking at the issue (Excellent analogy!)... still a little confused about one issue though.... regarding the event at the serial port.... If various threads are executing in the application will the system get a chance to acknowledge the/an event (which I am expecting at some point) that may take place due to something arriving at the serial port? Or when one thread looses control another thread will immediately take control and due to this the possible event will be missed or seriously delayed? Is there a way around this or am I just missing the point about something? If you could also explain this I'd be most grateful. maria

        1 Reply Last reply
        0
        • C Colin Angus Mackay

          maria_p wrote: say again multiple threads are running in an application, in one thread I implement a lock on a certain portion of code (so this must complete execution before any other thread can resume control)... Now, what I remember from my concurrency lectures (8 to 10 years ago) if you lock a piece of code (the "critical section") other code in other threads can still execute as they are not preventing from executing, they just can't execute the locked code. Think of the critical section as the centre of a highway intersection with an American 4-way Stop sign. (In America the rule is the first to arrive has right of way and can pass through the intersection). So the first car arrives and is allowed onto the intersection. A fraction of a second later another car arrives from a different direction, it must wait until the first car has passed through. This is locking. Now, just because some traffic has to wait to gain access to this intersection doesn't stop the traffic flowing on an interstate a mile away. Does this help you understand how concurrency (multithreading) works? --Colin Mackay--

          EuroCPian Spring 2004 Get Together[^] "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Colin Angus Mackay wrote: Now, what I remember from my concurrency lectures (8 to 10 years ago) if you lock a piece of code (the "critical section") other code in other threads can still execute as they are not preventing from executing, they just can't execute the locked code. This depends on a number of factors. First, it depends on what you're locking against (if using the lock keyword). As I explained to her before, if you want to lock a block of code for all threads, then a static object (say, the Type of the class that contains the definition) should be used. If she uses an instance variable (such as this), then only the method will be locked for that instance no matter how many threads reference the class. If you use the lock keyword, a Monitor is used which does block successive calls by different threads. The only way for threads to not wait is to use Monitor.TryEnter or use a Mutex and call WaitOne with a value to set the timeout.

          -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

          1 Reply Last reply
          0
          • M maria_p

            Hi Folks, I wonder if somebody can either point me in the right direction or explain a certain concept to me... i just can't figure it out (i've browssed through MSDN)...If in an application I have several threads running and while these threads are running something arrives on the serial port of the computer...Using a control I can raise an event to signal this "BUT" will the application be aware that this event has taken place? Or how can I make sure that the application is aware that something has been recieved at the serial port while other threads are running?.... Please can someone help... A secondary issue also related ... say again multiple threads are running in an application, in one thread I implement a lock on a certain portion of code (so this must complete execution before any other thread can resume control)... if a timer raises an event (or is due to raise an event) while another thread is executing a portion of locked code... will this timer event be lost by the time the locked code in the other thread completes??? can anyone help!!! I'm desperate... can't find this information anywhere... Maria (phillips_maria@hotmail.com)

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            Don't forget about all the articles in the .NET Framework SDK besides those in the class library. There is a pretty big selection of articles about threading in .NET at http://msdn.microsoft.com/library/en-us/cpguide/html/cpconthreading.asp[^] which also includes a lot of examples. Many of the overviews should even discuss threading in general for those not familiar with threads.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            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