TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Posix Threads Programming

165 点作者 dvaun超过 4 年前

11 条评论

37ef_ced3超过 4 年前
Here is an example:<p>NN-512 uses pthreads. It generates a tiny, standalone, highly scalable C99 work-stealing thread pool in every C file. The work items are simply coordinates, similar to the threadIdx&#x2F;blockIdx coordinates in Nvidia&#x27;s CUDA. For example, all the structs and functions starting with &quot;Example32Threader&quot; in this file:<p><a href="https:&#x2F;&#x2F;nn-512.com&#x2F;example&#x2F;32#3" rel="nofollow">https:&#x2F;&#x2F;nn-512.com&#x2F;example&#x2F;32#3</a><p>The implementation ends at Example32ThreaderDo1<p>The key is to avoid writing to shared cache lines whenever possible, and to keep a bitmap in the central struct (the &quot;hub&quot;) that avoids rediscovery of the fact that a thread has no remaining work to be stolen (thereby avoiding a &quot;thundering herd&quot; type problem: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Thundering_herd_problem" rel="nofollow">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Thundering_herd_problem</a>)<p>Another key is to never take any short-cuts in synchronization. Rather than doing something subtle&#x2F;clever to avoid mutexes (etc.), instead make sure the units of work are big enough so that synchronization costs are negligible with respect to the overall computation
评论 #26094831 未加载
marmight超过 4 年前
For folks interested in threads in C, I also recommend reading &quot;Threads Cannot be Implemented as a Library&quot; [1]. The summary is that Pthreads mostly works correctly as a library (1) because its functions execute memory fence instructions and (2) because its functions are treated as opaque functions by the C compiler, i.e., functions that might read or write from any global variable. However, these properties do not generate thread-safe code under a number of conditions such as under the presence of certain compiler optimizations. Thus, the paper argues that the compiler must be aware of threads and that threads cannot be implemented purely as a library.<p>[1] <a href="https:&#x2F;&#x2F;www.hpl.hp.com&#x2F;techreports&#x2F;2004&#x2F;HPL-2004-209.pdf" rel="nofollow">https:&#x2F;&#x2F;www.hpl.hp.com&#x2F;techreports&#x2F;2004&#x2F;HPL-2004-209.pdf</a>
评论 #26090088 未加载
评论 #26091560 未加载
rramadass超过 4 年前
Good succinct tutorial. For people interested in studying <i>Concurrency</i> (Threading is just a subset of the model) i highly suggest the following book;<p><i>Foundations of Multithreaded, Parallel, and Distributed Programming by Gregory Andrews</i>.<p>On the low-level OS&#x2F;Hardware side (i.e. SMP&#x2F;Caches etc.) <i>UNIX Systems for Modern Architectures: Symmetric Multiprocessing and Caching for Kernel Programmers by Curt Schimmel</i> is excellent.
评论 #26088134 未加载
MisterTea超过 4 年前
Concurrency is easy if the library is well thought out and clean. So I much prefer the Plan 9 approach using the CSP thread(2) library: <a href="http:&#x2F;&#x2F;man.postnix.pw&#x2F;9front&#x2F;2&#x2F;thread" rel="nofollow">http:&#x2F;&#x2F;man.postnix.pw&#x2F;9front&#x2F;2&#x2F;thread</a>, Example program: <a href="https:&#x2F;&#x2F;github.com&#x2F;mischief&#x2F;9problems&#x2F;blob&#x2F;master&#x2F;sys&#x2F;src&#x2F;libthread&#x2F;example.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mischief&#x2F;9problems&#x2F;blob&#x2F;master&#x2F;sys&#x2F;src&#x2F;li...</a><p>Wonderful easy to use library which is built on top of rfork and uses channels and alternates to synchronize the procs. Heap memory can be shared across processes (e.g. malloc&#x27;d memory or a segment(3)) so passing data around is just passing pointers to objects in shared memory.<p>Further reading which demonstrates the library and how it influenced Go can be found here: <a href="https:&#x2F;&#x2F;seh.dev&#x2F;go-legacy&#x2F;" rel="nofollow">https:&#x2F;&#x2F;seh.dev&#x2F;go-legacy&#x2F;</a>
zerof1l超过 4 年前
Pthreads will always have special place in my heart. It was my first serious introduction to the parallelism concepts.
评论 #26085976 未加载
评论 #26088369 未加载
albmoriconi超过 4 年前
This wonderful tutorial is the same I used to learn Pthreads around 10 years ago.<p>The training section of the LLNL website [1] offers a lot of solid tutorials; I used the OpenMP and MPI ones for my Parallel Computing class some years ago and they helped me to sistematically learn (and learn to apply) the material. I love them!<p>[1] <a href="https:&#x2F;&#x2F;hpc.llnl.gov&#x2F;training&#x2F;tutorials" rel="nofollow">https:&#x2F;&#x2F;hpc.llnl.gov&#x2F;training&#x2F;tutorials</a>
intc超过 4 年前
Nice share - Very clear and informative. Wrote some time ago a simple threaded implementation of Sieve of Eratosthenes utilizing POSIX threads (in C): <a href="https:&#x2F;&#x2F;github.com&#x2F;intc&#x2F;c-notes&#x2F;blob&#x2F;master&#x2F;threads&#x2F;4-thrd_eratosthenes&#x2F;erat.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;intc&#x2F;c-notes&#x2F;blob&#x2F;master&#x2F;threads&#x2F;4-thrd_e...</a>
pantulis超过 4 年前
This is a great resource, and it&#x27;s also well written. I did a lot of C thread programming 22 years ago (plus some CORBA), and this would have been very helpful, basically resumes a whole semester of university classes in one web page!<p>Lazy question: are there any details of how pthreads are implemented for each operating system?
bch超过 4 年前
I recently got excited about OpenMP[0], which seems to let one take advantage of threads while handling some of the drudgery of setup in common patterns.<p>[0] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;OpenMP" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;OpenMP</a><p>Edit: add link
turminal超过 4 年前
Every threading tutorial should come with a warning. This one doesn&#x27;t, so I&#x27;m adding one here.<p><a href="https:&#x2F;&#x2F;shouldiusethreads.com" rel="nofollow">https:&#x2F;&#x2F;shouldiusethreads.com</a>
screwgoth超过 4 年前
Wow !! This is the same page from where I started learning about Pthreads way back in 2004. 16 freaking years ago. I loved it then, love it now.