开发者

What to use for Thread synchronization (in c#)

currently I am using lock for sending\receiving the file from FTP through TCPClient class.

But this supports only single file to send and receive开发者_Python百科.

So what's the better way to use in this situation (lock, mutex, interlock, read\write lock, semaphore).


Let's say you have a list of files you want to transfer in parallel (one thread for each file) and want to wait until all the uploads are finished, you could create an array of WaitHandles (number of wait handles == number of files == number of threads) and pass each thread one of them (wait handle : thread == 1:1) along with the file the thread should transfer.

The thread would transfer the file and before it exists, it would set the wait handle.

In the "main thread" you could wait for all the wait handles from the array to be set using WaitHandle.WaitAll(WaitHandle[]) (I may not be correct about the exact syntax here, but a command like this exists, I'm sure. I've used it before).

This is called a barrier.


Im not sure your situation can be solved by any of those locking contexts. You might want to look into Asynchronous I/O and its various implementations, but specifically in regards to the WebRequest and Webresponse classes.

You did not say whether you were using the default classes for handling your FTP or a custom library. I (perhaps incorrectly) assume you are creating the FTP handlers yourself. If you are using a third party library, you should check to see if they support Async IO, or multithreading in another form.


You can use semaphore it is a classic example to create a number of concurrent works.
http://www.dijksterhuis.org/using-semaphores-in-c/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜