简体   繁体   English

将pthread转换为Objective-C

[英]convert pthread to objective-c

Im trying to convert the following to objective-c code. 我试图将以下内容转换为objective-c代码。

This is the current thread I have in C and works fine 这是我在C拥有的当前线程,并且工作正常

//calling EnrollThread method on a thread in C
pthread_t thread_id; 
pthread_create( &thread_id, NULL, EnrollThread, pParams );

//What the EnrollThread method structure looks like in C
void* EnrollThread( void *arg )

What my method structure looks like now that I've changed it to objective-c 现在我将其更改为Objective-C时,我的方法结构看起来像什么

-(void)enrollThreadWithParams:(LPBIOPERPARAMS)params;

Now I'm not sure how to call this objective-c method with the pthread_create call. 现在,我不确定如何通过pthread_create调用此objective-c方法。 I've tried something like this: 我已经尝试过这样的事情:

pthread_create( &thread_id, NULL, [refToSelf enrollThreadWithParams:pParams], pParams );

But I believe I have it wrong. 但我相信我做错了。 Can anyone enlighten me on why this does not work and what it is I need to do to fix it so that I can create my thread in the background? 谁能启发我为什么这行不通,我需要做些什么来修复它,以便可以在后台创建线程? My UI is getting locked until the method finishes what it's doing. 我的UI被锁定,直到该方法完成其工作为止。

I was thinking of also using dispatch_sync but I haven't tried that. 我当时也在考虑使用dispatch_sync但我还没有尝试过。

In objective C you don't really use pthread_create , although you can still use it, but the thread entry point needs to be a C function, so I'm not sure if this would be the best approach. 在目标C中,虽然仍然可以使用pthread_create ,但实际上并没有使用它,但是线程入口点必须是C函数,因此我不确定这是否是最佳方法。

There are many options, as you can read in the Threading and Concurrency documents. 您可以在“ 线程并发”文档中阅读许多选项。

  • performSelectorInBackground method of NSObject (and subclasses) NSObject(和子类)的performSelectorInBackground方法
  • dispatch_async (not dispatch_sync as you mentioned) dispatch_async (不是您提到的dispatch_sync
  • NSOperation and NSOperationQueue NSOperation和NSOperationQueue
  • NSThread class NSThread类

I would suggest giving it a shot to the first one, since it is the easiest, and very straightforward, also the second one is very easy because you don't have to create external objects, you just place inline the code to be executed in parallel. 我建议您对第一个进行一下尝试,因为它是最简单,最直接的方法,而第二个也非常简单,因为您不必创建外部对象,只需将要执行的代码内联即可平行。

The go to reference for concurrent programming is the Concurrency Programming Guide which walks you through dispatch queues (known as Grand Central Dispatch, GCD) and operation queues. 并发编程的参考参考是《 并发编程指南》 ,该指南将引导您遍历调度队列(称为Grand Central Dispatch,GCD)和操作队列。 Both are incredibly easy to use and offer their own respective advantages. 两者都非常易于使用,并具有各自的优势。

In their simplest forms, both of these are pretty easy to use. 以它们最简单的形式,这两个都非常易于使用。 As others have pointed out, the process for creating a dispatch queue and then dispatching something to that queue is: 正如其他人指出的那样,创建调度队列然后向该队列调度对象的过程是:

dispatch_queue_t queue = dispatch_queue_create("com.domain.app", DISPATCH_QUEUE_CONCURRENT);

dispatch_async(queue, ^{
    // something to do in the background
});

The operation queue equivalent is: 操作队列等效项是:

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[queue addOperationWithBlock:^{
    // something to do in the background
}];

Personally, I prefer operation queues where: 就个人而言,我更喜欢以下操作队列:

  • I need controlled/limited concurrency (ie I'm going to dispatch a bunch of things to that queue and I want them to run concurrent with respect to not only the main queue, but also with respect to each other, but I don't want more than a few of those running simultaneously). 我需要受控/有限的并发性(即,我将一堆东西分派到该队列,并且我希望它们不仅相对于主队列而且相对于彼此并发运行,但是我不想要多个同时运行)。 A good example would be when doing concurrent network requests, where you want them running concurrently (because you get huge performance benefit) but you generally don't want more than four of them running at any given time). 一个很好的例子是在进行并发网络请求时,您希望它们并发运行(因为可以获得巨大的性能优势),但通常在任何给定时间都不要运行四个以上的请求。 With an operation queue, one can specify maxConcurrentOperationCount whereas this tougher to do with GCD. 使用操作队列,可以指定maxConcurrentOperationCount而使用GCD则更难。

  • I need fine level of control over dependencies. 我需要对依赖项进行精细控制。 For example, I'm going to start operations A, B, C, D, and E, but B is dependent on A (ie B shouldn't start before A finishes), D is dependent upon C, and E is dependent upon both B and D finishing. 例如,我将开始操作A,B,C,D和E,但是B依赖于A(即B不应在A完成之前开始),D依赖于C,E依赖于B和D都完成。

  • I need to enjoy concurrency on tasks that, themselves, run asynchronously. 我需要在异步运行的任务上享受并发性。 Operations offer a fine degree of control over what determines when the operation is to be declared as isFinished with the use of NSOperation subclass that uses "concurrent operations". 通过使用使用“并发操作”的NSOperation子类,操作可以很好地控制确定何时将操作声明为isFinishedNSOperation A common example is the network operation which, if you use the delegate-based implementation, runs asynchronously, but you still want to use operations to control the flow of one to the next. 一个常见的示例是网络操作,如果您使用基于委托的实现,该操作将异步运行,但是您仍然希望使用操作来控制一个到另一个的流程。 The very nice networking library, AFNetworking, for example, uses operations extensively, for this reason. 因此,非常好的网络库AFNetworking广泛使用了操作。

On the other hand, GCD is great for simple one-off asynchronous tasks (because you can avail yourself of built-in "global queues", freeing yourself from making your own queue), serial queues for synchronizing access to some shared resource, dispatch sources like timers, signaling between threads with semaphores, etc. GCD is generally where people get started with concurrent programming in Cocoa and Cocoa Touch. 另一方面,GCD非常适合简单的一次性异步任务(因为您可以利用内置的“全局队列”,使自己不必创建自己的队列),串行队列以同步访问某些共享资源,调度GCD通常是人们开始在Cocoa和Cocoa Touch中进行并发编程的地方。

Bottom line, I personally use operation queues for application-level asynchronous operations (network queues, image processing queues, etc.), where the degree of concurrency becomes and important issue). 最重要的是,我个人将操作队列用于应用程序级别的异步操作(网络队列,图像处理队列等),在这些操作中,并发度成为重要问题。 I tend to use GCD for lower-level stuff or quick and simple stuff. 我倾向于将GCD用于较低级别的内容或快速简单的内容。 GCD (with dispatch_async ) is a great place to start as you dip your toe into the ocean of concurrent programming, so go for it. 当您将脚趾伸入并发编程的海洋时,GCD(带有dispatch_async )是一个不错的起点。

There are two things I'd encourage you to be aware of, regardless of which of these two technologies you use: 无论您使用哪种两种技术,我都鼓励您注意以下两点:

  1. First, remember that (in iOS at least) you always want to do user interface tasks on the main queue. 首先,请记住(至少在iOS中)您总是要在主队列上执行用户界面任务。 So the common patterns are: 因此,常见的模式是:

     dispatch_async(queue, ^{ // do something slow here // when done, update the UI and model objects on the main queue dispatch_async(dispatch_get_main_queue(), ^{ // UI and model updates can go here }); }); 

    or 要么

     [queue addOperationWithBlock:^{ // do something slow here // when done, update the UI and model objects on the main queue [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // do UI and model updates here }]; }]; 
  2. The other important issue to consider is synchronization and "thread-safety". 要考虑的另一个重要问题是同步和“线程安全性”。 (See the Synchronization section of the Threading Programming Guide. ) You want to make sure that you don't, for example, have the main thread populating some table view while, at the same time, some background queue is changing the data used by that table view at the same time. (请参阅《 线程编程指南》中的“ 同步”部分 )例如,您要确保不要让主线程填充某些表视图,而同时某些后台队列正在更改用于以下操作的数据:该表视图同时显示。 You want to make sure that while any given thread is using some model object or other shared resource, that another thread isn't mutating it, leaving it in some inconsistent state. 您要确保在任何给定线程正在使用某个模型对象或其他共享资源的同时,另一个线程不会对其进行突变,从而使其处于不一致状态。

There's too much to cover in the world of concurrent programming. 并发编程领域有太多东西要讨论。 The WWDC videos (including 2011 and 2012) offer some great background on GCD and asynchronous programming patterns, so make sure you avail yourself of that great resource. WWDC视频(包括2011年和2012年)提供了有关GCD和异步编程模式的丰富背景知识,因此请确保您可以充分利用该丰富资源。

If you already have working code, there is no reason to abandon pthreads. 如果您已经有了有效的代码,则没有理由放弃pthread。 You should be able to use it just fine. 您应该可以使用它。

If, however, you want an alternative, but you want to keep your existing pthread entry point, you can do this easily enough... 但是,如果您想要替代方法,但又想保留现有的pthread入口点,则可以轻松完成此操作...

dispatch_queue_t queue = dispatch_queue_create("EnrollThread", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
    EnrollThread(parms);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM