2

The method GetItemSearchResponse will be called by multiple console application. But I wanted to call this method one by one. So I applied mutex. So that this method will be locked for other threads.

  public class AWSItemSearchClient : IDisposable
        {
            // the name of the global mutex;
            private const string MutexName = "FAA9569-7DFE-4D6D-874D-19123FB16CBC-8739827-[SystemSpecicString]";

            private Mutex _globalMutex;

            private bool _owned = false;

    //This method call should be synchronized
      public ItemSearchResponse GetItemSearchResponse(ItemSearch itemSearch)
            {
                ItemSearchResponse response = null;

                RequestAgain:
                try
                {
                    _globalMutex = new Mutex(true, MutexName, out _owned);
                    while (!_owned)
                    {
                        // did not get the mutex, wait for it.
                        _owned = _globalMutex.WaitOne(2000);
                    }

                    AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();

                    response = amazonClient.ItemSearch(itemSearch);


                    Thread.Sleep(2000);
               AppLogger.ExamineThreadAcquisitionLog("Lock acquired by Thread " + Process.GetCurrentProcess().Id + " Exe: " + AppDomain.CurrentDomain);
                }
                catch (Exception ex)
                {
                    AppLogger.ExamineThreadAcquisitionLog("Error in Item Search request : " + ex.Message);
                    goto RequestAgain;
                }

                return response;
            }

            public void Dispose()
            {
                if (_owned)
                {
                    _globalMutex.ReleaseMutex();
                    Thread.Sleep(1500);
                    AppLogger.ExamineThreadAcquisitionLog("Lock released by Thread " + Process.GetCurrentProcess().Id + " Exe: " + AppDomain.CurrentDomain);
                }
                _globalMutex = null;
            }
        }

But the log which I am getting is not convincing the proper execution of code.

  ----
Lock acquired by Thread 9916 Exe: Name:FB.ABC.vshost.exe
There are no context policies.

12/28/2016 5:27:06 PM
----
Lock Released by Thread 17396 Exe: Name:FB.XYZ.vshost.exe
There are no context policies.

12/28/2016 5:27:06 PM
----
Lock Released by Thread 9916 Exe: Name:FB.ABC.vshost.exe
There are no context policies.

12/28/2016 5:27:09 PM
----
Lock acquired by Thread 17396 Exe: Name:FB.XYZ.vshost.exe
There are no context policies.

12/28/2016 5:27:10 PM
----
Lock acquired by Thread 9916 Exe: Name:FB.ABC.vshost.exe
There are no context policies.

It should be like one thread got lock and it should be released by that thread first then the lock should be acquired by other thread.

I am calling this method in console apps as following :

 using (var client = new AWSItemSearchClient())
 {
   response = client.GetItemSearchResponse(itemSearch);
 }

Also the above method is called by 3 console apps but the lock is acquired always by two threads, 1 and 9. How can I distribute locks equally?

Manjay_TBAG
  • 2,176
  • 3
  • 23
  • 43
  • But you don't log process id. How do you know that those threads do not belong to different processes? Thread id is not globally unique, only unique in given process. – Evk Dec 28 '16 at 11:20
  • Is this not the processId Thread.CurrentThread.ManagedThreadId? – Manjay_TBAG Dec 28 '16 at 11:47
  • No, it's a thread id (as its name suggsets). To get current process id you need "Process.GetCurrentProcess().Id" – Evk Dec 28 '16 at 11:49
  • I have modified log statement and seen that for initial few minutes locks are acquired interchangeably by two processes but after that only one process is acquiring the lock and third process is never able to acquire the lock. – Manjay_TBAG Dec 28 '16 at 12:03

0 Answers0