Ios 有没有办法让调度队列在单线程中工作?

Ios 有没有办法让调度队列在单线程中工作?,ios,grand-central-dispatch,nsoperationqueue,nsthread,Ios,Grand Central Dispatch,Nsoperationqueue,Nsthread,这是我的密码: @interface MyObject () @property(nonatomic) dispatch_queue_t queue; @end @implementation MyObject { NSThread *_check; } - (id)init { self = [super init]; if (self) { _queue = dispatch_queue_create("com.Thread.queue", NUL

这是我的密码:

@interface MyObject ()
@property(nonatomic) dispatch_queue_t queue;
@end

@implementation MyObject {
    NSThread *_check;
}

- (id)init {
    self = [super init];
    if (self) {
        _queue = dispatch_queue_create("com.Thread.queue", NULL);
        dispatch_async(_queue, ^{
            _check = [NSThread currentThread]; //for ex. thread number = 3
            //some code here...
        });
    }

    return self;
}

- (void)someMethod:(MyObjClass *)obj {
    dispatch_async(_queue, ^{
        //need th
        if (_check != [NSThread currentThread]) { // it is sometimes number 3, but sometimes it changes
            NSLog(@"Thread changed.");
        }
        [obj doSmth]; //got crash if currentThread != _check         
    });
}

@end

我需要确保所有MyObjClass的方法都在同一个线程中执行。但这段代码会根据自己的意愿改变线程,但有时它在单线程中工作。我能强迫它一直使用同一个线程吗?

如果你有多个类实例,你会用每个新的init覆盖你的队列


可以使用单例或静态来解决此问题。

总之,不是。除了主队列之外,GCD没有任何线程关联的概念。如果您确实需要线程关联性,那么GCD并不是真正合适的工具。如果你喜欢这个成语,并且想“适应”一些东西来满足你的需要,你可以这样做:

@implementation AppDelegate
{
    NSThread* thread;
}

void dispatch_thread_async(NSThread* thread, dispatch_block_t block)
{
    if ([NSThread currentThread] == thread)
    {
        block();
    }
    else
    {
        block = [block copy];
        [(id)block performSelector: @selector(invoke) onThread: thread withObject: nil waitUntilDone: NO];
    }
}

void dispatch_thread_sync(NSThread* thread, dispatch_block_t block)
{
    if ([NSThread currentThread] == thread)
    {
        block();
    }
    else
    {
        [(id)block performSelector: @selector(invoke) onThread: thread withObject: nil waitUntilDone: YES];
    }
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    thread = [[NSThread alloc] initWithTarget: self selector:@selector(threadMain) object:nil];
    [thread start];

    dispatch_thread_async(thread, ^{
        NSLog(@"Async Thread: %@", [NSThread currentThread]);
    });

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        dispatch_thread_sync(thread, ^{
            NSLog(@"Sync Thread: %@", [NSThread currentThread]);
        });
    });
}

- (void)threadMain
{
    // You need the NSPort here because a runloop with no sources or ports registered with it
    // will simply exit immediately instead of running forever.
    NSPort* keepAlive = [NSPort port];
    NSRunLoop* rl = [NSRunLoop currentRunLoop];
    [keepAlive scheduleInRunLoop: rl forMode: NSRunLoopCommonModes];
    [rl run];
}

@end

是的,你说得对。但这不是问题所在,每个实例都有不同的线程是可以的。在我的例子中,我在init之后设置了_check,只有当
_check!=[NSThread currentThread]
.Thx,这正是我想要的。还有一件事需要提及-它的工作速度比仅使用GCD的样本慢约10%。是的,GCD是高度优化的。我很惊讶罚球只有10%。