有什么方法可以使dispatch_queue_t在单线程中工作? [英] Any way to make dispatch_queue_t work in single thread?

查看:113
本文介绍了有什么方法可以使dispatch_queue_t在单线程中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

@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的方法都在同一线程中执行.但是此代码可以根据自己的意愿更改线程,但有时它可以在单线程中工作.我有什么办法可以强迫它一直使用同一线程?

I need to make sure that all MyObjClass's methods performs in the same thread. But this code change thread by it's own will, but sometimes it work in single thread. Any way I can force it to use same thread all the time?

推荐答案

总之,没有.除了主队列之外,GCD没有任何线程相似性的概念.如果您确实需要线程关联,那么GCD并不是真正的正确工具.如果您喜欢这种惯用语,并且想要某种适应"您所需要的东西,则可以执行以下操作:

In a word, no. Other than the main queue, GCD does not have any notion of thread affinity. If you really need thread affinity, GCD is not really the right tool. If you like the idiom, and want to kind of "adapt" something to your needs you could do something like this:

@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

这篇关于有什么方法可以使dispatch_queue_t在单线程中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆