我如何开始使用多线程编程?

时间:2022-12-17 15:03:47

I am a beginner on Stack Overflow. I am working on a Unix platform in C/C++. Knowing basic programming in these regards how could I start with multithreading?

我是Stack Overflow的初学者。我正在使用C / C ++开发Unix平台。在这些方面了解基本编程我怎样才能从多线程开始?

Multithreading seems to be very interesting and I want to grow my knowledge in this regard.

多线程似乎非常有趣,我想在这方面发展我的知识。

How could I get started with multithreading and what are the best techniques/books/ebooks/articles available to grab the concepts as early as possible?

我怎样才能开始使用多线程,哪些是最好的技术/书籍/电子书/文章可以尽早获取概念?

11 个解决方案

#1


8  

Study regarding pthread, mutexes and try to implement same that will be beneficial for you.

关于pthread,互斥体的研究并尝试实现对你有益的。

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

#2


7  

Learning multi threading programming has two parts:

学习多线程编程有两个部分:

  1. How to write multi threading applications
  2. 如何编写多线程应用程序

  3. How to use the available API (pthread)
  4. 如何使用可用的API(pthread)

Learning multi-threaded programming is harder, thre's a good article published in the Linux Journal that will help you understand the basic principles.

学习多线程编程更难,这是Linux Journal上发表的一篇好文章,它将帮助您理解基本原理。

To better understand pThreads I suggest reading this tutorial - POSIX Threads Programming

为了更好地理解pThreads,我建议阅读本教程 - POSIX Threads Programming

There is also a good book by O'rielly called PThreads Programming

O'rielly还有一本名为PThreads Programming的好书

#3


2  

If you're getting started with multithreading, my advice would be to first review and better understand I/O on your system. Understand blocking vs. non-blocking I/O, signaling, asynchronous routines, callbacks et cetera. I/O is probably one, if not the primary, reason for adding multithreading to your programs. With that knowledge you can then pick up a book on pthreads or java threads, or wrap your mind around the Boost threads library or another threading library for your favorite technology.

如果您开始使用多线程,我的建议是首先检查并更好地了解系统上的I / O.了解阻塞与非阻塞I / O,信令,异步例程,回调等等。 I / O可能是将多线程添加到程序中的一个原因(如果不是主要原因)。有了这些知识,您就可以在pthreads或java线程上找到一本书,或者将您的思想包裹在Boost线程库或其他线程库中,以获得您喜欢的技术。

#4


1  

I think that the Wikipedia article Multithreading give you a quick overview and by following the external links you'll get a good overview of the topic. After that - or additionally - you could read Tanenbaum's Operating Systems: Design and Implementation (great book by the way). But the most important thing is - in my opinion - to get your hands on it. So just download a sample application from... let's say The Code Project or whatever website you'll find and play around with it. See how the application differs if you use locks or what happens if two threads try to access the same resource and how often this will occur, etc. By that I think you'll get the hang of it pretty quickly. And it's fun to evaluate and play around with techniques that are new to oneself.

我认为*文章多线程为您提供了快速概述,通过跟踪外部链接,您将对该主题有一个很好的概述。在那之后 - 或者另外 - 你可以阅读Tanenbaum的操作系统:设计和实现(顺便说一句好书)。但最重要的是 - 在我看来 - 要抓住它。所以只需从...下载一个示例应用程序,让我们说代码项目或您将找到的任何网站,并使用它。如果您使用锁定或者如果两个线程试图访问相同的资源以及发生的频率会发生什么,请查看应用程序的不同之处等等。我认为您很快就能掌握它。评估和玩弄自己的新技术很有趣。

#5


1  

I found this tutorial very informative and clearly-written. Hope it is helpful.

我发现本教程内容翔实且写得很清楚。希望它有所帮助。

#6


0  

Maybe a bit controversial, but multithreading really cklicked for me when I attempted to solve a coding puzzle once.

也许有点争议,但当我试图解决一次编码拼图时,多线程真的很适合我。

The puzzle was about writing thread safe code without using mutexes. My first attempts were miserable but when I finally got it, it was like learning to ride a bike - I've never felt unsure about concurrency ever since.

难题是关于编写线程安全代码而不使用互斥锁。我的第一次尝试很悲惨,但是当我终于得到它时,就像学骑自行车一样 - 从那以后我从未对并发感到不确定。

Some times I've even stumbled upon programmers who have read books on the subject, but fails to understand simple things like the fact that a primitive assignment may sometimes not be an atomic operation.

有时候我甚至偶然发现了那些阅读过有关该主题的书籍的程序员,但却无法理解简单的事情,例如原始赋值有时可能不是原子操作。

#7


0  

Everything depends on you goals. There is plenty of code and articles with common multi-threading problems solved based on POSIX threads framework (I see number of recommendations of good articles here). The main question is what do you want to build. For some tasks it is not recommended to go multi-threading at all.

一切都取决于你的目标。基于POSIX线程框架解决了大量具有常见多线程问题的代码和文章(我在这里看到好文章的推荐数量)。主要问题是你想要建立什么。对于某些任务,建议不要进行多线程处理。

Here is book "Foundations of Multithreaded, Parallel, and Distributed Programming" which is related to discussed topic and which I'd like to recommend. The most significant advantage of it is 'relatively easy to read' style but no hard linkage to POSIX threads ideology (which is common problem).

这是“多线程,并行和分布式编程的基础”一书,它与讨论的主题相关,我想推荐。它最显着的优点是“相对容易阅读”的风格,但没有与POSIX线程意识形态的硬连接(这是常见的问题)。

#8


0  

If you want to study the details try reading Advanced Programming in the UNIX Environment. or start with Computer Systems: A Programmer's Perspective.

如果您想学习详细信息,请阅读UNIX环境中的高级编程。或者从计算机系统开始:程序员的观点。

#9


0  

Something else to try: http://www.threadingbuildingblocks.org

还有其他尝试:http://www.threadingbuildingblocks.org

#10


0  

since it's UNIX why not start with processes and IPC comms? i.e. message queues, shared memory and mutexes.

因为它是UNIX,为什么不从进程和IPC通信开始?即消息队列,共享内存和互斥锁。

#11


0  

Studying various library frameworks and O/S facilities is a good way to understand low-level concurrency. Examples you find there can get you started writing concurrent code in a short amount of time.

研究各种库框架和O / S工具是理解低级并发的好方法。您在那里找到的示例可以让您在很短的时间内开始编写并发代码。

After you debug your way through a few deadlocks and shared memory corruption issues, you will find that you need some tools for reasoning about and decomposing your concurrency problems. I personally like Herb Sutter's The Pillars of Concurrency article as a starting point. The idea is to better understand why you need concurrency. Are you looking for improved response time, parallel calculation, some combination of reasons?

在通过一些死锁和共享内存损坏问题调试之后,您会发现需要一些工具来推理和分解并发问题。我个人喜欢Herb Sutter的The Pillars of Concurrency文章作为起点。这个想法是为了更好地理解为什么需要并发。您是否在寻找改进的响应时间,并行计算,某些原因组合?

Understanding the "why" will lead you to a better "what". From there you can always spider out to different low-level approaches: Active Objects, monitor objects, message passing, etc. As you said, there is a lot to know in this field.

理解“为什么”会引导你找到更好的“什么”。从那里你总是可以选择不同的低级方法:活动对象,监视对象,消息传递等。正如你所说,在这个领域有很多东西要知道。

#1


8  

Study regarding pthread, mutexes and try to implement same that will be beneficial for you.

关于pthread,互斥体的研究并尝试实现对你有益的。

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

#2


7  

Learning multi threading programming has two parts:

学习多线程编程有两个部分:

  1. How to write multi threading applications
  2. 如何编写多线程应用程序

  3. How to use the available API (pthread)
  4. 如何使用可用的API(pthread)

Learning multi-threaded programming is harder, thre's a good article published in the Linux Journal that will help you understand the basic principles.

学习多线程编程更难,这是Linux Journal上发表的一篇好文章,它将帮助您理解基本原理。

To better understand pThreads I suggest reading this tutorial - POSIX Threads Programming

为了更好地理解pThreads,我建议阅读本教程 - POSIX Threads Programming

There is also a good book by O'rielly called PThreads Programming

O'rielly还有一本名为PThreads Programming的好书

#3


2  

If you're getting started with multithreading, my advice would be to first review and better understand I/O on your system. Understand blocking vs. non-blocking I/O, signaling, asynchronous routines, callbacks et cetera. I/O is probably one, if not the primary, reason for adding multithreading to your programs. With that knowledge you can then pick up a book on pthreads or java threads, or wrap your mind around the Boost threads library or another threading library for your favorite technology.

如果您开始使用多线程,我的建议是首先检查并更好地了解系统上的I / O.了解阻塞与非阻塞I / O,信令,异步例程,回调等等。 I / O可能是将多线程添加到程序中的一个原因(如果不是主要原因)。有了这些知识,您就可以在pthreads或java线程上找到一本书,或者将您的思想包裹在Boost线程库或其他线程库中,以获得您喜欢的技术。

#4


1  

I think that the Wikipedia article Multithreading give you a quick overview and by following the external links you'll get a good overview of the topic. After that - or additionally - you could read Tanenbaum's Operating Systems: Design and Implementation (great book by the way). But the most important thing is - in my opinion - to get your hands on it. So just download a sample application from... let's say The Code Project or whatever website you'll find and play around with it. See how the application differs if you use locks or what happens if two threads try to access the same resource and how often this will occur, etc. By that I think you'll get the hang of it pretty quickly. And it's fun to evaluate and play around with techniques that are new to oneself.

我认为*文章多线程为您提供了快速概述,通过跟踪外部链接,您将对该主题有一个很好的概述。在那之后 - 或者另外 - 你可以阅读Tanenbaum的操作系统:设计和实现(顺便说一句好书)。但最重要的是 - 在我看来 - 要抓住它。所以只需从...下载一个示例应用程序,让我们说代码项目或您将找到的任何网站,并使用它。如果您使用锁定或者如果两个线程试图访问相同的资源以及发生的频率会发生什么,请查看应用程序的不同之处等等。我认为您很快就能掌握它。评估和玩弄自己的新技术很有趣。

#5


1  

I found this tutorial very informative and clearly-written. Hope it is helpful.

我发现本教程内容翔实且写得很清楚。希望它有所帮助。

#6


0  

Maybe a bit controversial, but multithreading really cklicked for me when I attempted to solve a coding puzzle once.

也许有点争议,但当我试图解决一次编码拼图时,多线程真的很适合我。

The puzzle was about writing thread safe code without using mutexes. My first attempts were miserable but when I finally got it, it was like learning to ride a bike - I've never felt unsure about concurrency ever since.

难题是关于编写线程安全代码而不使用互斥锁。我的第一次尝试很悲惨,但是当我终于得到它时,就像学骑自行车一样 - 从那以后我从未对并发感到不确定。

Some times I've even stumbled upon programmers who have read books on the subject, but fails to understand simple things like the fact that a primitive assignment may sometimes not be an atomic operation.

有时候我甚至偶然发现了那些阅读过有关该主题的书籍的程序员,但却无法理解简单的事情,例如原始赋值有时可能不是原子操作。

#7


0  

Everything depends on you goals. There is plenty of code and articles with common multi-threading problems solved based on POSIX threads framework (I see number of recommendations of good articles here). The main question is what do you want to build. For some tasks it is not recommended to go multi-threading at all.

一切都取决于你的目标。基于POSIX线程框架解决了大量具有常见多线程问题的代码和文章(我在这里看到好文章的推荐数量)。主要问题是你想要建立什么。对于某些任务,建议不要进行多线程处理。

Here is book "Foundations of Multithreaded, Parallel, and Distributed Programming" which is related to discussed topic and which I'd like to recommend. The most significant advantage of it is 'relatively easy to read' style but no hard linkage to POSIX threads ideology (which is common problem).

这是“多线程,并行和分布式编程的基础”一书,它与讨论的主题相关,我想推荐。它最显着的优点是“相对容易阅读”的风格,但没有与POSIX线程意识形态的硬连接(这是常见的问题)。

#8


0  

If you want to study the details try reading Advanced Programming in the UNIX Environment. or start with Computer Systems: A Programmer's Perspective.

如果您想学习详细信息,请阅读UNIX环境中的高级编程。或者从计算机系统开始:程序员的观点。

#9


0  

Something else to try: http://www.threadingbuildingblocks.org

还有其他尝试:http://www.threadingbuildingblocks.org

#10


0  

since it's UNIX why not start with processes and IPC comms? i.e. message queues, shared memory and mutexes.

因为它是UNIX,为什么不从进程和IPC通信开始?即消息队列,共享内存和互斥锁。

#11


0  

Studying various library frameworks and O/S facilities is a good way to understand low-level concurrency. Examples you find there can get you started writing concurrent code in a short amount of time.

研究各种库框架和O / S工具是理解低级并发的好方法。您在那里找到的示例可以让您在很短的时间内开始编写并发代码。

After you debug your way through a few deadlocks and shared memory corruption issues, you will find that you need some tools for reasoning about and decomposing your concurrency problems. I personally like Herb Sutter's The Pillars of Concurrency article as a starting point. The idea is to better understand why you need concurrency. Are you looking for improved response time, parallel calculation, some combination of reasons?

在通过一些死锁和共享内存损坏问题调试之后,您会发现需要一些工具来推理和分解并发问题。我个人喜欢Herb Sutter的The Pillars of Concurrency文章作为起点。这个想法是为了更好地理解为什么需要并发。您是否在寻找改进的响应时间,并行计算,某些原因组合?

Understanding the "why" will lead you to a better "what". From there you can always spider out to different low-level approaches: Active Objects, monitor objects, message passing, etc. As you said, there is a lot to know in this field.

理解“为什么”会引导你找到更好的“什么”。从那里你总是可以选择不同的低级方法:活动对象,监视对象,消息传递等。正如你所说,在这个领域有很多东西要知道。