我正在使用 dispatch_semaphore_wait 来停止我当前的线程,但看起来它停止了我的所有线程。
代码:
SampleReader *reader = [[SampleReader alloc] initWithHostname:hostname andFilePath:filepath];
reader.endHandler = endHandler;
[reader start];
dispatch_semaphore_wait(reader->mSem, DISPATCH_TIME_FOREVER);
我的启动方法类似于:
mFileStream = [[NSOutputStream outputStreamToFileAtPath:[fileurl path] append:FALSE] retain];
[mFileStream open];
mNetworkStream = (NSInputStream *)CFReadStreamCreateWithFTPURL(NULL, (CFURLRef)ftpurl);
mNetworkStream.delegate = self;
[mNetworkStream scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[mNetworkStream open];
我在其中一个委托方法中获得回调,其中我发出信号量
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{
switch (eventCode)
Case NSStreamEventErrorOccurred:
dispatch_semaphore_signal(mSem);
break;
case NSStreamEventEndEncountered:
dispatch_semaphore_signal(mSem);
break;
}
但是,当我在信号量上发送等待时,不会调用委托方法。仅当我注释掉该行时才调用它
//dispatch_semaphore_signal(mSem);
任何帮助将不胜感激。