macos C:多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7774778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: Multithreading
提问by madtapper
Is multithreading supported in C? If yes, then how do I try? Is there any open source library that lets me do it and is the library supported on Mac OS X?
C 支持多线程吗?如果是,那么我该如何尝试?是否有任何开源库可以让我这样做,Mac OS X 是否支持该库?
I haven't found any article saying that it's supported.
我还没有找到任何文章说它受支持。
回答by Dave
C is not intrinsically a multithreaded language; however there are many libraries which add threading functionality.
C 本质上不是多线程语言;然而,有许多库添加了线程功能。
pthreads
is a library compatible with any POSIX system, so it is supported on OSX. I found https://computing.llnl.gov/tutorials/pthreads/to be a good place to start.Win32 has a threading library for C described at http://msdn.microsoft.com/en-us/library/y6h8hye8(v=vs.80).aspx.
Glib
adds threading supported, and has the advantage of being completely cross-platform, as long as glib is installed on the target machine. There is some information here: http://developer.gnome.org/glib/2.28/glib-Threads.html
pthreads
是一个与任何 POSIX 系统兼容的库,因此它在 OSX 上受支持。我发现https://computing.llnl.gov/tutorials/pthreads/是一个很好的起点。Win32 有一个 C 线程库,在http://msdn.microsoft.com/en-us/library/y6h8hye8(v=vs.80).aspx 中有描述。
Glib
增加了线程支持,并且具有完全跨平台的优点,只要目标机器上安装了glib。这里有一些信息:http: //developer.gnome.org/glib/2.28/glib-Threads.html
回答by stdcall
the new dialect - C1X, will offer multi-threading out of the box, as stated from wikipedia:
正如维基百科所述,新方言 - C1X 将提供开箱即用的多线程:
Multithreading support (_Thread_local storage-class specifier, header including thread creation/management functions, mutex, condition variable and thread-specific storage functionality, as well as the _Atomic type qualifier and for uninterruptible object access).
多线程支持(_Thread_local 存储类说明符,标头包括线程创建/管理函数、互斥锁、条件变量和线程特定的存储功能,以及 _Atomic 类型限定符和用于不间断对象访问)。
currently of courae as mentioned above, multi-threading is not supported in the newest dialect of c - C99
目前如上所述,c - C99 的最新方言不支持多线程
回答by Adrian Cornish
C has no concept whatever of threads. There is no thread support in C Standard. There are extensions available that can implement multi threading - one of which is pthreads.
C 没有任何线程的概念。C 标准中没有线程支持。有一些扩展可以实现多线程——其中之一是 pthreads。
Be aware because C language has no natural support of threads you as the programmer have to take care of everything and you will not be protected against any of the pitfalls of multi-threaded programming.
请注意,因为 C 语言没有对线程的自然支持,您作为程序员必须照顾一切,并且您不会受到多线程编程的任何陷阱的保护。
回答by JWWalker
I would guess that the majority of multithreaded programming on Mac OS X is done in Objective-C or C++, not plain C. (I realize that this isn't exactly an answer to the question that you asked, but you might want to know about alternatives.) In Objective-C, you'd use NSThread or, in Snow Leopard and later, Grand Central Dispatch (GCD). In C++, you could use the threads library from boost.org, which has the advantage of being cross-platform.
我猜想 Mac OS X 上的大部分多线程编程都是用 Objective-C 或 C++ 完成的,而不是普通的 C。关于替代方案。)在 Objective-C 中,您将使用 NSThread,或者在 Snow Leopard 和更高版本中使用 Grand Central Dispatch (GCD)。在 C++ 中,您可以使用 boost.org 中的线程库,它具有跨平台的优点。
回答by invaliddata
Pthreads. OSX has posix support.
线程。OSX 有 posix 支持。