Semaphore 이용환 Download 완료 확인
CS/Common 2010. 1. 8. 07:10원문 : Stanford iTunesU Programming Paradigm 17강
01 | int DownloadSingleFile( const char * server, const char * path); |
02 | int DownloadAllFiles( const char * server, const char * files[], int n) |
03 | { |
04 | int totalBytes = 0; |
05 |
06 | Semaphore lock = 1; |
07 | Semaphore childrenDone = 0; |
08 |
09 | for ( int i=0; i < n; ++i) |
10 | { |
11 | ThreadNew( "DownloadSingleFile" , DownloadSingleFile, 4, server, files[i], & |
12 | totalBytes, lock, childrenDone); |
13 | } |
14 | for ( int i=0; i < n; ++i) |
15 | { |
16 | // wait return. |
17 | SemaphoreWait(childrenDone); |
18 | } |
19 |
20 | return totalBytes; |
21 | } |
22 |
23 | void DownloadHelper( const char * server, const char * path, |
24 | int * returnButes, Semaphore lock, Semaphore parentToSignal) |
25 | { |
26 | int bytesDownloaded = DownloadSingleFile(server, path); |
27 | SemaphoreWait(lock); |
28 | *numBytes += bytesDownloaded; |
29 | SemaphoreSignal(lock); |
30 | SemaphoreSignal(parentToSignal); |
31 | } |