使用NSOperationQueue进行同步通信

时间:2011-06-03 12:30:44

标签: objective-c

我是Objective C编程的新手。

我使用NSInvocationOperation创建了两个名为add and display的线程,并将其添加到NSOperationQueue

我首先运行显示线程,然后运行添加线程。打印“欢迎使用显示”后的显示线程必须等待结果从add方法打印。

所以我设置了waitUntilFinished方法。

两个操作都在同一个队列中。如果我在同一队列上使用waitUntilFinished进行操作,可能会出现死锁的情况(来自apple developer文档)。是这样吗?

要等待特定时间间隔,有一种名为waitUntilDate:的方法 但如果我需要这样wait(min(100,dmax));让我dmax = 20;如何等待这些条件?

如果有人能用一个例子来解释,那会很有帮助。

编辑:

threadss.h
------------

#import <Foundation/Foundation.h>


@interface threadss : NSObject {

    BOOL m_bRunThread;
    int a,b,c;
    NSOperationQueue* queue;
    NSInvocationOperation* operation;
    NSInvocationOperation* operation1;
    NSConditionLock* theConditionLock;

}
-(void)Thread;
-(void)add;
-(void)display;
@end

threadss.m
------------

#import "threadss.h"

@implementation threadss

-(id)init
{
    if (self = [super init]) {
     queue = [[NSOperationQueue alloc]init];
     operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(display) object:nil];
     operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(add) object:nil];
     theConditionLock = [[NSConditionLock alloc]init];
    }
    return self;
}
-(void)Thread
{
    m_bRunThread = YES;

    //[operation addDependency:operation1];
    if (m_bRunThread) {
    [queue addOperation:operation];
    }
    //[operation addDependency:operation1];
    [queue addOperation:operation1];

    //[self performSelectorOnMainThread:@selector(display) withObject:nil waitUntilDone:YES];
    //NSLog(@"I'm going to do the asynchronous communication btwn the threads!!");
    //[self add];
    //[operation addDependency:self];
    sleep(1);
    [queue release];
    [operation release];
    //[operation1 release];
}
-(void)add
{
    NSLog(@"Going to add a and b!!");
    a=1;
    b=2;
    c = a + b;
    NSLog(@"Finished adding!!");
}
-(void)display
{
    NSLog(@"Into the display method");
    [operation1 waitUntilFinished];
    NSLog(@"The Result is:%d",c);

}
@end


main.m
-------
#import <Foundation/Foundation.h>
#import "threadss.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    threadss* thread = [[threadss alloc]init];
    [thread Thread];

    [pool drain];
    return 0;
}

这是我尝试过的示例程序。

输出

2011-06-03 19:40:47.898 threads_NSOperationQueue[3812:1503] Going to add a and b!!
2011-06-03 19:40:47.898 threads_NSOperationQueue[3812:1303] Into the display method
2011-06-03 19:40:47.902 threads_NSOperationQueue[3812:1503] Finished adding!!
2011-06-03 19:40:47.904 threads_NSOperationQueue[3812:1303] The Result is:3

调用线程的方式是否正确。

1.会有任何死锁情况吗?

2.如何等待(min(100,dmax)),其中dmax = 50。

2 个答案:

答案 0 :(得分:2)

假设我正确理解你的问题,你有两个操作:

  1. 操作A:打印消息,等待操作B完成,继续
  2. 操作B:打印信息
  3. 如果是这种情况,你能打印第一条消息,开始操作B,然后开始操作A?

    此外,当您使用NSOperationQueue时,您不直接管理线程,它会为您执行所有线程管理。因此,在您提出'线程'的问题时,您实际上是想说“操作”。


    直接回答你的问题,“这会导致僵局”,是的,它可以。如果将队列更改为顺序而不是并发,或者如果使操作2依赖于操作1,则可能会锁定。我建议不要尝试做你正在做的事情,重构你的代码,这样一个操作不需要暂停,而另一个操作。根据您发布的代码,没有理由像这样构建您的代码。

答案 1 :(得分:0)

希望这对您有所帮助,它是Windows中的WaitForSingleObject的iOS版本:

dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[object runSomeLongOperation:^{
    // your own code here.
    dispatch_semaphore_signal(semaphore);
}];

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
dispatch_release(semaphore);
相关问题