Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用NSOperationQueue的同步通信_Objective C - Fatal编程技术网

Objective c 使用NSOperationQueue的同步通信

Objective c 使用NSOperationQueue的同步通信,objective-c,Objective C,我不熟悉目标C编程 我使用NSInvocationOperation创建了两个名为add和display的线程,并将其添加到NSOperationQueue中 我首先运行显示线程,然后运行添加线程。打印“欢迎显示”后的显示线程必须等待从add方法打印结果 因此,我设置了waitUntilFinished方法 这两个操作都在同一队列上。如果我对同一队列上的操作使用waitUntilFinished,可能会发生死锁(来自Apple开发人员文档)。是这样吗 要等待特定的时间间隔,有一个名为waitUn

我不熟悉目标C编程

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

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

因此,我设置了
waitUntilFinished
方法

这两个操作都在同一队列上。如果我对同一队列上的操作使用
waitUntilFinished
,可能会发生死锁(来自Apple开发人员文档)。是这样吗

要等待特定的时间间隔,有一个名为
waitUntilDate:
但是如果我需要喜欢这个
等待(min(100,dmax))let
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;
}
调用线程的方式是否正确

1.是否存在死锁情况


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

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

  • 操作A:打印消息,等待操作B完成,然后继续
  • 操作B:打印一条消息
  • 如果是这种情况,您可以打印第一条消息,启动操作B,然后启动操作A吗

    另外,当您使用
    NSOperationQueue
    时,您不直接管理线程,它会为您完成所有的线程管理。所以在你的问题中,当你说“线程”时,你实际上是想说“操作”



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

    希望这对您有所帮助,它是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);
    

    请用问号表示问题。@marcelo:对不起,从我的下一个问题开始就可以了。在Obj-C中,所有的类都应该以大写字母开头,所有的方法都应该以小写字母开头。@kubi:谢谢。会像你说的那样更改它。另外,在这段代码中,你根本不会直接使用线程,你在做手术。这两个操作可能在同一个线程上运行,也可能在不同的线程上运行,但是这一级别的细节被抽象掉了;你很可能不想那样做。您试图完成什么?抱歉,我正在尝试线程(添加和显示)的示例代码。我有一个cpp代码要移植到目标C。在代码waitForSingleObject中有一个用于等待事件和时间延迟。我需要等待100毫秒。希望可以使用waitUntilDate进行此操作:
    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);