并发编程 C++?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/218786/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:49:09  来源:igfitidea点击:

Concurrent programming c++?

c++concurrencyc++11

提问by yesraaj

I keep on hearing about concurrent programing every where. Can you guys throw some light on what it's and how c++ new standards facilitate doing the same?

我不断听到关于并发编程的消息。你们能解释一下它是什么以及 c++ 新标准如何促进做同样的事情吗?

回答by Anthony Williams

Concurrency is about your code doing multiple things at the same time. This is typically done with explicit "threads", but there are other possibilities. For example, if you use OpenMP directives in your code then a compiler that supports OpenMP will automatically generate threads for you.

并发是关于你的代码同时做多项事情。这通常是通过显式“线程”完成的,但还有其他可能性。例如,如果您在代码中使用 OpenMP 指令,那么支持 OpenMP 的编译器将自动为您生成线程。

Thread is short for "thread of execution". In a single-threaded C++ program, execution starts at main(), and then proceeds in a sequential fashion. In a multi-threaded program, the first thread starts at main, but additional threads may be started by the application which start at a user-specified function. These then run concurrently, or in parallel with the original thread.

线程是“执行线程”的缩写。在单线程 C++ 程序中,执行从 main() 开始,然后按顺序进行。在多线程程序中,第一个线程从 main 开始,但其他线程可能由应用程序启动,这些线程从用户指定的函数开始。然后这些线程并发运行,或与原始线程并行运行。

In C++0x threads are started using the std::threadclass:

在 C++0x 中,线程是使用std::thread类启动的:

void my_function()
{
    // do stuff
}
std::thread my_thread(my_function); // run my_function in its own thread

The new C++0x standard also supports:

新的 C++0x 标准还支持:

  • atomic values and operations with the std::atomic<>class template,
  • mutexes for data protection (std::mutex, std::recursive_mutex, etc.)
  • lock classes for ease of managing lock lifetime (std::lock_guard<>, std::unique_lock<>)
  • std::lockand std::try_lockfunctions to manage acquiring multiple locks at the same time without risking deadlock
  • condition variables to ease waiting for an event (std::condition_variable, std::condition_variable_any)
  • futures, promises and packaged tasks to simplify passing data between threads, and waiting for a value to be ready. This addresses the classic "how do I return a value from a thread" question.
  • thread-safe initialization of local static objects
  • the thread_localkeyword to declare thread-local data
  • 使用std::atomic<>类模板的原子值和操作,
  • 用于数据保护的互斥(std::mutexstd::recursive_mutex等)
  • 易于管理锁生命周期的锁类 ( std::lock_guard<>, std::unique_lock<>)
  • std::lockstd::try_lock管理同时获取多个锁而不会冒死锁风险的功能
  • 条件变量以缓解等待事件 ( std::condition_variable, std::condition_variable_any)
  • 期货,承诺和打包任务,以简化线程之间的数据传递,以及等待值准备就绪。这解决了经典的“如何从线程返回值”问题。
  • 本地静态对象的线程安全初始化
  • thread_local声明线程本地数据的关键字

I gave a more detailed overview of the new C++0x thread library in my article on devx.com: Simpler Multithreading in C++0x

我在 devx.com 上的文章中更详细地概述了新的 C++0x 线程库:C++0x 中的简单多线程

I write about multithreading and concurrency in C++ on my blog. I'm also writing a book on the topic: C++ Concurrency in Action.

我在我的博客上写了 C++ 中的多线程和并发性。我也在写一本关于这个主题的书:C++ Concurrency in Action

回答by Michael Burr

When you say "how c++ new standards facilitate" concurrent programming, I assume you're talking about the soon (?) to be released C++09 standard.

当您说“c++ 新标准如何促进”并发编程时,我假设您是在谈论即将发布的 (?) C++09 标准。

The new standard as it currently stands in draft form supports the following items that help with concurrent programming:

目前处于草案形式的新标准支持以下有助于并发编程的项目:

  • atomic types and addresses
  • a thread class
  • thread_local storage (which was just added into the draft standard a few months ago)
  • mutual exclusion (mutex classes)
  • condition variables - this is particularly nice for Windows, since condition variables are difficult to implement correctly in Win32. This means that eventually Microsoft should provide support for condition variables at least in the MSVC++ runtime, so it will be easy to get correct condition variable semantics on WIn32.
  • 原子类型和地址
  • 线程类
  • thread_local 存储(几个月前刚刚添加到标准草案中)
  • 互斥(互斥类)
  • 条件变量——这对 Windows 来说特别好,因为条件变量很难在 Win32 中正确实现。这意味着微软最终应该至少在 MSVC++ 运行时提供对条件变量的支持,因此很容易在 WIn32 上获得正确的条件变量语义。

回答by Joel Martinez

回答by Paul Nathan

Concurrency is having multiple threads of execution for a given process. As of today, C++ does not directly support it. However, several libraries exist that will tie a given function to a new thread of execution. The Unix standard is the pthreads library.

并发是给定进程有多个执行线程。截至今天,C++ 不直接支持它。但是,存在几个库将给定的函数绑定到新的执行线程。Unix 标准是 pthreads 库。

回答by Dynite

C++CSP2 - Easy Concurrency for C++

C++CSP2 - C++ 的简单并发

http://www.cs.kent.ac.uk/projects/ofa/c++csp/

http://www.cs.kent.ac.uk/projects/ofa/c++csp/

CSP is a based on a proper concurrent paradigm as opposed to threads and locks and all other manner of things which are tacked on as an afterthought.

CSP 是基于适当的并发范式,而不是线程和锁以及所有其他作为事后考虑的事物。

(See Occam-Pi for a concurrent programming language (also based on CSP))

(有关并发编程语言(也基于 CSP),请参阅 Occam-Pi)

回答by Nick

My slightly different take, specific to future directions of programming paradigms:

我略有不同的看法,具体到未来的编程范式方向:

Concurrency is about writing your program such that it can be doing multiple things at once if the hardware supports it. Currently, most languages have fairly heavy and complicated mechanisms to allow the programmer to specify this (eg: threads with manual synchronization, OpenMP pre-processor directives, etc.).

并发是关于编写你的程序,如果硬件支持它,它可以同时做多件事。目前,大多数语言都有相当繁重和复杂的机制来允许程序员指定这一点(例如:具有手动同步的线程、OpenMP 预处理器指令等)。

As hardware improves, it's going to improve horizontally (more cores) rather than vertically (faster single core). This means apps will need to have "latent concurrency" in order to scale with "faster" hardware. Languages are currently trying to evolve to best support this, to be in the position of best language for future development.

随着硬件的改进,它将在水平方向(更多内核)而不是垂直方向(更快的单核)进行改进。这意味着应用程序需要具有“潜在并发性”才能使用“更快”的硬件进行扩展。语言目前正在努力发展以最好地支持这一点,以成为未来发展的最佳语言。

C++0x is adding more built-in support for the "old" methods of programming concurrency. Various compiler vendors are adding "new" methods which abstract the threading model and allow run-time decisions on numbers of threads, etc. (based on the hardware of the machine); for Microsoft in particular, see F#, concurrency runtime, parallel extensions, etc.

C++0x 正在为编程并发的“旧”方法添加更多内置支持。各种编译器供应商正在添加“新”方法,这些方法抽象了线程模型并允许对线程数量等进行运行时决策(基于机器的硬件);特别是对于 Microsoft,请参阅 F#、并发运行时、并行扩展等。

Hope that helps.

希望有帮助。

回答by Marcus Thornton

This is the best article to understand concurrent programming: Concurrent Programming

这是了解并发编程的最佳文章:并发编程

You will get the full picture of concurrent programming and C++ after reading it.

阅读本书后,您将全面了解并发编程和 C++。

As a quick summary, we can say that concurrent programming is to do multitasking. When a program gets blocked, it can do other things. Typically we get blocked while waiting for network connections and dealing with I/O. We can facilitate concurrent programming using fork()and thread libraries.

作为一个快速总结,我们可以说并发编程就是做多任务。当程序被阻止时,它可以做其他事情。通常我们在等待网络连接和处理 I/O 时会被阻塞。我们可以使用fork()线程库来促进并发编程。