Synchronization

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rakesh_nits

    #1

    Synchronization

    Hi All,
    I am having a function which can be called from different independent
    threads
    simultaneously. Inside this i am having a hashtable.what each thread
    will do that it will check whether the hashtable contains a particular
    key if not then it will make an entry in the hashtable with that key or
    if it exists it will simply use the value corresponding to that
    particular key.what i want that the access to the function should be on
    first come first serve basis.so if a thread is already waiting on the
    function the current thread after completing processing should leave it
    (should enter in succession only if there is not any waiting thread),
    and no thread should be kept waiting for infinite.so entry into the
    function should be related to time of waiting in short i want the
    impementation to be a queue like.is using lock(object) will be suffice
    for the task or i have to use something else.What is the best way to
    accomlish this?. and also if you can tell me how this lock statement
    functions.wheth er it will cause the other statement to wait or simply
    not allow the other threads to access it and throw some error.Thanks in
    Advance.
    Rakesh

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Synchronization

    Rakesh,

    The lock statement would give you what you want if it wasn't for the
    requirement to have a timeout.

    However, you can do the following:

    // Assume o is the object you want to lock on. Wait for 5 seconds.
    Monitor.TryEnte r(o, 5000);

    // Try/catch.
    try
    {
    // Perform code here.
    }
    finally
    {
    // Release lock.
    Monitor.Exit(o) ;
    }

    This is basically what the compiler does with a lock statement, sans the
    call to TryEnter. Rather, it replaces it with Enter.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "rakesh_nit s" <cool.rakesh@gm ail.com> wrote in message
    news:1136955652 .743384.98150@g 49g2000cwa.goog legroups.com...[color=blue]
    > Hi All,
    > I am having a function which can be called from different independent
    > threads
    > simultaneously. Inside this i am having a hashtable.what each thread
    > will do that it will check whether the hashtable contains a particular
    > key if not then it will make an entry in the hashtable with that key or
    > if it exists it will simply use the value corresponding to that
    > particular key.what i want that the access to the function should be on
    > first come first serve basis.so if a thread is already waiting on the
    > function the current thread after completing processing should leave it
    > (should enter in succession only if there is not any waiting thread),
    > and no thread should be kept waiting for infinite.so entry into the
    > function should be related to time of waiting in short i want the
    > impementation to be a queue like.is using lock(object) will be suffice
    > for the task or i have to use something else.What is the best way to
    > accomlish this?. and also if you can tell me how this lock statement
    > functions.wheth er it will cause the other statement to wait or simply
    > not allow the other threads to access it and throw some error.Thanks in
    > Advance.
    > Rakesh
    >[/color]


    Comment

    • Stoitcho Goutsev \(100\)

      #3
      Re: Synchronization

      rakesh_nits,

      ..NET doesn't have out-of-the-box solution for this.

      There couple types that you can use for synchornizing threads, but non of
      the, AFAIKm guarantees that the threads will enter the guarded section in a
      FCFS basis. Which means that that a thread may wait infinitelly if the app
      is too busy.

      However using this unordered Monitor you can build ordered sync object.

      Here is my idea:
      1. All call this new type FCFSMonitor
      2. It has the follwong methods:
      - Constructor that except an object to use for sync the guarded section
      - Enter method
      - Exit method
      3. When you create instance of this class you pass some reference to a type
      (reference type) and it saves this reference as an internal field. You also
      create a queue of objects that will be used to keep the order of threads
      trying to enter the critical section.
      4. When a thread want so enter the critucal section it calls
      fcfsMontior.Ent er method. This method does the following thing:
      - Calls Montior.TryEnte r on the object passed in the constructor. If the
      TryEnter fails you create a new Object, lock the object, adds it to the
      queue and the call Monitor.Wait on this object.
      - If a threads enters the critical section it do whatever it needs to do
      and call fcfsMontior.Exi t. The exit method gets the next object from the
      queue lock it, call Monitor.Pusle on it (to wake up the waiting thread),
      remove the object from the queue and unlock it. Then leaves the critical
      section by unlocking the main sync object. The awaken thread should first
      unlock its object and then try to enter the critical section in the same way
      it did before.

      Ofcourse this alogorithm is not perfect and needs to be polished, but I
      think is a good start.


      --
      HTH
      Stoitcho Goutsev (100)

      "rakesh_nit s" <cool.rakesh@gm ail.com> wrote in message
      news:1136955652 .743384.98150@g 49g2000cwa.goog legroups.com...[color=blue]
      > Hi All,
      > I am having a function which can be called from different independent
      > threads
      > simultaneously. Inside this i am having a hashtable.what each thread
      > will do that it will check whether the hashtable contains a particular
      > key if not then it will make an entry in the hashtable with that key or
      > if it exists it will simply use the value corresponding to that
      > particular key.what i want that the access to the function should be on
      > first come first serve basis.so if a thread is already waiting on the
      > function the current thread after completing processing should leave it
      > (should enter in succession only if there is not any waiting thread),
      > and no thread should be kept waiting for infinite.so entry into the
      > function should be related to time of waiting in short i want the
      > impementation to be a queue like.is using lock(object) will be suffice
      > for the task or i have to use something else.What is the best way to
      > accomlish this?. and also if you can tell me how this lock statement
      > functions.wheth er it will cause the other statement to wait or simply
      > not allow the other threads to access it and throw some error.Thanks in
      > Advance.
      > Rakesh
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Synchronization

        rakesh_nits wrote:[color=blue]
        > I am having a function which can be called from different independent
        > threads
        > simultaneously. Inside this i am having a hashtable.what each thread
        > will do that it will check whether the hashtable contains a particular
        > key if not then it will make an entry in the hashtable with that key or
        > if it exists it will simply use the value corresponding to that
        > particular key.what i want that the access to the function should be on
        > first come first serve basis.so if a thread is already waiting on the
        > function the current thread after completing processing should leave it
        > (should enter in succession only if there is not any waiting thread),
        > and no thread should be kept waiting for infinite.so entry into the
        > function should be related to time of waiting in short i want the
        > impementation to be a queue like.is using lock(object) will be suffice
        > for the task or i have to use something else.What is the best way to
        > accomlish this?. and also if you can tell me how this lock statement
        > functions.wheth er it will cause the other statement to wait or simply
        > not allow the other threads to access it and throw some error.Thanks in
        > Advance.[/color]

        Have a look at
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


        Currently it doesn't have a "TryLock" instead of Lock, but it wouldn't
        be hard to add (and you don't need an explicity try/finally block that
        way).

        Jon

        Comment

        Working...