开发者

Sleeping a worker thread in a file server

I am implementing a file server and it receives requests from multiple clients. Each client sends multiple requests. At the server end, the main thread spawns a new worker thread to handle requests each time a new client connects. One worke开发者_如何学编程r thread handles all requests from the client it was created for. So, after the thread handles a requests, it waits to be woken up by the main thread when another request from the same client arrives.

I am not sure how to implement the last line.that is how do i put a thread to sleep and wake it again.

thanks


Use condition variables https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal


You do not "put a thread to sleep". A thread will sleep when it makes any blocking system call.

There are many ways to implement what you describe, the most common perhaps being the "Thread pool pattern". This is usually implemented using a thread-safe queue, which in turn can be implemented with mutexes and condition variables.

The worker thread will go to sleep when it waits on the condition variable (pthread_cond_wait), and the master will wake it up by signaling the condition variable (pthread_cond_signal).

A little searching should turn up a number of pthread-based sample code for queues.


Do your client-handling threads hold any state for the client? If so, I can understand why you would need one, dedicated thread for each client. If this is the case, you could use a producer-consumer queue for each client-handler thread as suggested by Cnicutar and other posters. I usually use a semaphore togehter with a mutex since such synchro is available on all platforms, but condvars OK. It is not clear how you identify the client when a file request comes in, so managing the client threads may be awkward, or not. When a file request comes in to the main thread, how do you know which queue to push it on?

Rgds, Martin


You should study the problem of producers-consumers. In a nutshell you want to do something like this:

  • Worker thread tries to get "job" from a queue
  • If the queue is empty, the thread blocks (semaphore / condition variable), otherwise it gets the job and processes it
  • The master queue puts jobs onto queues and notifies workers ("hey, there's work to do")
  • A notified worker instantly wakes up and tries to get a job from the queue

The trick is to do it real elegant:

  • the get should automatically block when the queue is empty
  • the put should automatically signal workers waiting on it

How would you do this with what Linux puts at your disposal ?

  • sempahores
  • pthreads condition variables

Now, back to your question. It seems slightly dubious that you start a thread for each client. It would be way simpler if you could have a thread pool sitting around and processing requests from any client.


I solved the issue by threads created at the beginning of a client connect and thereafter the thread receives and sends messages directly to client rather then the main thread receiving and sending messages after the worker threads does the task.


You have several possibilities, but I implemented a similar multithreaded solution as follows:

  • Allow your worker thread's method to return when it has no work to do
  • When your parent thread receives a request, add it to the worker thread's queue, and call a "Wakeup" method, along the lines of:

    private void Wakeup()
    {
        if (!_workerThread.IsBusy)
            _workerThread.RunWorkerAsync();
    }
    

This will allow you to not have your thread hung in sleep state, where it would need to regularly check to see if a wakeup has been requested. This puts the responsibility of waking up the worker thread onto the parent thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜