并发编程c ++? [英] Concurrent programming c++?

查看:146
本文介绍了并发编程c ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直听说关于并发编程的每一个地方。

解决方案

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



线程是执行线程的缩写。在单线程C ++程序中,执行开始于main(),然后以顺序方式进行。在多线程程序中,第一个线程在main处开始,但是附加线程可以由应用程序启动,该程序从用户指定的函数开始。



在C ++中,0x线程是使用 std :: thread class:

  void my_function()
{
// do stuff
}
std :: thread my_thread(my_function); //在其自己的线程中运行my_function

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




  • 使用 std :: atomic<> 类模板的原子值和操作, li>
  • 用于数据保护的互斥体( std :: mutex std :: recursive_mutex std :: lock_guard<> 等)。

  • std :: unique_lock<>

  • std :: lock
  • 条件变量以缓解等待事件()。

    std :: condition_variable
    std :: condition_variable_any

  • 期货,承诺和打包任务线程之间的数据,以及等待值准备就绪。

  • 本地静态对象的线程安全初始化

  • thread_local 关键字来声明线程本地数据



新的C ++ 0x线程库在我的文章上devx.com:更简单的多线程C ++ 0x

我在我的博客上写了关于C ++中的多线程和并发性。我还在撰写有关此主题的书籍: C ++并发操作


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?

解决方案

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.

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.

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

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:

  • 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::lock and std::try_lock functions 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_local keyword to declare thread-local data

I gave a more detailed overview of the new C++0x thread library in my article on devx.com: Simpler Multithreading in 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 ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆