1

hi i want to build a simple program which does

  Parallel.ForEach(m_CustomerArr, customer =>
        {
            customer.OrderSomthing();
        });

when customer order something he query database, when his item run out of he contact to the supplier

      public bool takeOrder(Order o)
    {
            lock (s_QueueLocker)
            {
                OrderQueue.Enqueue(o);
            }
            //waits here to be signaled by the working thread of supplier 
            o.m_ManualResetEvent.WaitOne();**
        return true;
    }  

there is only one supplier who holdes a queue who have a supplierthread:

   public void CreateThead()
    {
        Order currentOrder;
        while (true)
        {
            if (OrderQueue.Count != 0)
            {
                currentOrder = OrderQueue.Dequeue();
                DoOrder(currentOrder); 
            }
        }

    }
     private void DoOrder(Order currentOrder)
    {
        //update the db this function runes only in one thread 
        currentOrder.m_ManualResetEvent.Set();
        System.Threading.Thread.Sleep(5000);
    }

where m_ManualResetEvent is a member of customer whom is passed to every order in c'tor

   public class Order
{
    public ManualResetEvent m_ManualResetEvent;
    public string m_Query;
    public int m_Quntity;

    public Order(ManualResetEvent customerManualResetEvent, string query, int quntity)
    {
        m_ManualResetEvent = customerManualResetEvent;
        m_Query = query;
        m_Quntity = quntity;
    }

}

what would be a the best way to stop the thread on wait (in **) and signal him like mutex in c ? I am new to c# multi-threading and my concept is from Linux kernel so things might be easier in C# in a class somebody already built whom implement it...

so my question is ' is there a better design , better practice , better way to do the above ?

5
  • 1
    Why are you updating the database in one thread? Commented Nov 12, 2010 at 19:44
  • @Pieter good question :) let's assume I create thread from Threadpool to deal with it ... good advice mate :) as I mentioned I lake an experience in data bases and c# fields Commented Nov 12, 2010 at 19:48
  • 1
    Thread from the thread pool? I was wondering: if you fire of a DB write on a different thread and then just wait for it; why don't you just do the DB write in the thread itself? Commented Nov 12, 2010 at 19:51
  • @Pieter it's a work i was given in job interview after i passed the first test I think the main think here it's that the supplier is one entity who have to take care of multiply clients sand he takes care of one each time , so he got only one thread, the work defention are not well defined so your earlier comment seems like a nice think i will ask them ... Commented Nov 12, 2010 at 19:56
  • BTW You can't use a thread pool because if you send more than one, it'll just create two threads. Commented Nov 12, 2010 at 19:58

1 Answer 1

1

If you are really just writing data to the database, you should see whether it's possible to write it in the thread itself and not on a separate thread. If that is even remotely possible, that will save you a lot of trouble.

Sign up to request clarification or add additional context in comments.

1 Comment

as I mentioned the supplier takes time to react (let's assume it's a far away server who takes a minute to do the order ...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.