简体   繁体   English

目前在Linux中进行线程编程的正确方法

[英]Current right way to do thread programming in Linux

I know that the implementation of threads in the Linux kernel and libc went through big changes in the past. 我知道Linux内核和libc中的线程实现过去经历了很大的变化。 What is the best way to use threads today from C programs? 今天在C程序中使用线程的最佳方法是什么? (I don't even know if there is more than one API that I can use -- I just know pthreads) (我甚至不知道是否有多个API可以使用 - 我只知道pthreads)

I don't care too much about old kernels and libc versions, but I do care about making efficient use of multiple cores and about portability (I may want my code to also work on other Unixes). 我不太关心旧内核和libc版本,但我确实关心有效使用多个内核和可移植性(我可能希望我的代码也可以在其他Unix上运行)。

If I just use Posix threads as described in man 7 pthreads and restrict my code to the POSIX API will that be OK? 如果我只使用man 7 pthreads描述的Posix线程并将我的代码限制在POSIX API就可以了吗?

edit : thanks to all who answerd. 编辑 :感谢所有回答的人。 I did think of using some available thread pool library, but for this project this really isn't an option. 我确实想过使用一些可用的线程池库,但对于这个项目,这真的不是一个选项。

For the most part, yes. 多半是对的。 That's the point of POSIX. 这就是POSIX的重点。 Each platform you plan to port to (including down to the OS, kernel number, and architecture) may have some differences that you'll need to be aware of. 您计划移植到的每个平台(包括操作系统,内核编号和体系结构)可能会有一些您需要注意的差异。 This is your homework :) 这是你的功课:)

Also, as a piece of advice, frameworks like Qt and library packages like Boost make a lot of this work more elegant. 此外,作为一条建议,像Qt这样的框架和像Boost这样的库包使得这项工作更加优雅。 If you can integrate them, I highly recommend it. 如果你可以整合它们,我强烈推荐它。

POSIX threads are a good choice and a popular one. POSIX线程是一个很好的选择和流行的。 You may also want to look at Boost Threads and Intel Thread Building Blocks for other options when programming in C/C++, rather than coding directly to the POSIX API. 在使用C / C ++编程时,您可能还希望查看Boost ThreadsIntel Thread Building Blocks以获取其他选项,而不是直接编写到POSIX API。 Specifically Intel TBB claims to use multi-core processors in an easy to use and efficient manner. 具体而言,英特尔TBB声称以易于使用和高效的方式使用多核处理器。 I don't have much experience with TBB, but I've seen demos where using TBB reduced code size and complexity dramatically and ran orders-of-magnitude faster than an implementation written in straight C++ using POSIX threads written by an experienced engineer. 我对TBB没有太多经验,但我已经看到使用TBB大大减少了代码大小和复杂性的演示,并且使用由经验丰富的工程师编写的POSIX线程在直接C ++中编写的实现速度快了几个数量级。

Using threads is difficult and error-prone. 使用线程很困难且容易出错。 Avoid programming them directly, if possible. 如果可能,请避免直接编程。 A well-written paper on the subject can be found here . 可以在这里找到关于这个主题的写得很好的论文。

Multicore parallelism can be achieved in a variety of different ways, other than direct use of threads. 除了直接使用线程之外,多核并行性可以以各种不同的方式实现。 The most straightforward is multi-process parallelism. 最直接的是多进程并行性。 For n cores, run n processes and subdivide the workload among them. 对于n个核心,运行n个进程并在其中细分工作负载。 This is most appropriate when tasks are coarse-grained and require little or no communication between them. 当任务粗粒度并且它们之间很少或根本不需要通信时,这是最合适的。 When it has to be in-process, you can use message-passing for all communication and synchronisation between threads and try to always pass immutable objects as messages. 当它必须在进程中时,您可以使用消息传递进行线程之间的所有通信和同步,并尝试始终将不可变对象作为消息传递。

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

相关问题 这是在linux编程中同时处理文件的有效方法吗? - Is this an efficient way to concurrently process files in linux programming? 编程线程 - Programming thread 是否有一种轻量级的方法来获取Linux中当前的进程数? - Is there a lightweight way to obtain the current number of Processes in Linux? 如何在 Linux 内核中打印当前线程堆栈跟踪? - How to print the current thread stack trace inside the Linux kernel? Linux内核中的current_thread_info()内联函数? - current_thread_info() inline function in Linux kernel? Linux内核模块编程中的_do_fork()问题 - Problem of _do_fork() in Linux kernel module programming 从文件逐行读取文件,并在Linux中的C编程中为文件中的每一行创建一个线程 - Read line by line from a file and create a thread for every line in a file in c programming in Linux 关于Cygwin的一些问题[Windows中的Linux](套接字,线程,其他编程和shell问题) - Some Issues About Cygwin[Linux in Windows] (socket,thread,other programming and shell issues) 在Linux内核中杀死进程的正确方法是什么? - What's the right way to kill a process in Linux Kernel? C / Linux编程:伪终端:如何从当前stdio重定向到pty并在使用后重定向 - C/Linux Programming: Pseudo-Terminals: how to redirect from current stdio to pty and redirect back after usage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM