与GCD相比,pthread是否提供任何优势? [英] Does pthreads provide any advantages over GCD?

查看:172
本文介绍了与GCD相比,pthread是否提供任何优势?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近学习了Grand Central Dispatch,我发现多线程代码非常直观(使用GCD).我喜欢不需要锁的事实(它内部使用无锁数据结构),而且API非常简单.

Having recently learned Grand Central Dispatch, I've found multithreaded code to be pretty intuitive(with GCD). I like the fact that no locks are required(and the fact that it uses lockless data structures internally), and that the API is very simple.

现在,我开始学习pthread,我不禁对复杂性有点不知所措.线程连接,互斥锁,条件变量-所有这些在GCD中都是不必要的,但是在pthread中有很多API调用.

Now, I'm beginning to learn pthreads, and I can't help but be a little overwhelmed with the complexity. Thread joins, mutexes, condition variables- all of these things aren't necessary in GCD, but have a lot of API calls in pthreads.

与GCD相比,pthread是否提供任何优势?效率更高吗?在正常情况下,pthread可以完成GCD无法完成的工作(内核级软件除外)吗?

Does pthreads provide any advantages over GCD? Is it more efficient? Are there normal-use cases where pthreads can do things that GCD can not do(excluding kernel-level software)?

就跨平台兼容性而言,我不太担心.毕竟,libdispatch是开源的,Apple已将其闭包更改更改为GCC的补丁程序,clang支持闭包,并且已经(例如FreeBSD),我们开始看到一些非Apple的GCD实现.我对使用API​​最为感兴趣(具体示例非常棒!).

In terms of cross-platform compatibility, I'm not too concerned. After all, libdispatch is open source, Apple has submtited their closure changes as patches to GCC, clang supports closures, and already(e.x. FreeBSD), we're starting to see some non-Apple implementations of GCD. I'm mostly interested in use of the API(specific examples would be great!).

推荐答案

您正在体验的那种压倒性感觉.这正是发明GCD的原因.

That overwhelming feeling that you are experiencing.. that's exactly why GCD was invented.

在最基本的级别上有线程,pthreads是用于线程的POSIX API,因此您可以在任何兼容的OS中编写代码,并期望它能工作. GCD建立在线程之上(尽管我不确定它们是否实际使用pthreads作为API).我相信GCD仅能在OS X和iOS上使用-简而言之,这是它的主要缺点.

At the most basic level there are threads, pthreads is a POSIX API for threads so you can write code in any compliant OS and expect it to work. GCD is built on top of threads (although I'm not sure if they actually used pthreads as the API). I believe GCD only works on OS X and iOS — that in a nutshell is its main disadvantage.

请注意,大量使用线程并需要高性能的项目会实现自己的线程池版本. GCD使您可以避免在无数次的情况下发明轮子.

Note that projects that make heavy use of threads and require high performance implement their own version of thread pools. GCD allows you to avoid (re)inventing the wheel for the umpteenth time.

这篇关于与GCD相比,pthread是否提供任何优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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