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 - Synchronization issues

MultiThreading - Synchronization issues

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.
  • M Offline
    M Offline
    madhusri
    wrote on last edited by
    #1

    Hi, My scenario is as follows: I spawn mukltiple threads simultaneously and all threads invoke functions simultaneously. These functions are in a single threaded application. What happens if one thread invokes the function and before it completes its processing, another thread also invoke it? Will both the threads execute the function simultaneously or will one thread complete its operation and only then the other will start processing?. In my case I want one specific function to be executed synchronously. Only one thread should execute it at a time because it makes use of certain resources whic can be used by only one thread at a time. How to lock the entire method? Can lock(this) { //Code goes here... } be used in this scenario. It would be better if any of you are able to suggest a solution for this.

    Thanks and Regards Madhu

    N 1 Reply Last reply
    0
    • M madhusri

      Hi, My scenario is as follows: I spawn mukltiple threads simultaneously and all threads invoke functions simultaneously. These functions are in a single threaded application. What happens if one thread invokes the function and before it completes its processing, another thread also invoke it? Will both the threads execute the function simultaneously or will one thread complete its operation and only then the other will start processing?. In my case I want one specific function to be executed synchronously. Only one thread should execute it at a time because it makes use of certain resources whic can be used by only one thread at a time. How to lock the entire method? Can lock(this) { //Code goes here... } be used in this scenario. It would be better if any of you are able to suggest a solution for this.

      Thanks and Regards Madhu

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello Forget the crap I said in my last post. I told you my mind was crashed back then. Anyway, here is the right thing. Two Approaches: Note: x is your object 1- Use the lock statement:

      lock(x)
      {
      DoSomething();
      }

      2- Use the

      System.Object obj = (System.Object)x;
      System.Threading.Monitor.Enter(obj);
      try
      {
      DoSomething();
      }
      finally
      {
      System.Threading.Monitor.Exit(obj);
      }

      The above codes are quoted from MSDN. I think you already got that by now, but I want to post that just in case you didn't.

      Regards:rose:

      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