c ++中的多线程 [英] Multi threading in c++

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

问题描述

我有两个函数task1()和task2()。现在我想做的是这两个函数应该同时执行并且两者都应该一次完成它的任务...所以请告诉我我是怎么做的可以做到。我需要完整的代码。

i have two functions task1() and task2().Now i want to do is that these two function should execute parallel at the same time and both should do its task at a time...So plz tell me how i can do it.I need complete code.

推荐答案

C ++没有嵌入语言的任何线程设施。它依赖于一些实现线程的API或库。它可以是特定的OS API,也可以是实现某些OS抽象层的库,即适用于许多OS。在这里,你有一些选择。其中一个图书馆很特别,因为它是标准化的。这是 C ++标准库

http:// en.wikipedia.org/wiki/C%2B%2B_Standard_Library [ ^ ],

http:// en。 wikipedia.org/wiki/C%2B%2B_Standard_Library#Thread_support_library [ ^ ]。



请注意,仅在C ++ 11中引入了线程类型:http://en.wikipedia.org/wiki/C%2B%2B11 [ ^ ]。



你可以找到一些许多地方的代码示例。例如: http://stackoverflow.com/questions/266168/simple-example-of -threading-in-c [ ^ ]。



如果你使用比这更旧的东西,你有很多选择:特定于操作系统的API(我建议你总是标记你的你问题中的平台)和许多替代方案,例如提升:

http:// en .wikipedia.org / wiki / Boost_library [ ^ ] ,

http://www.boost.org [ ^ ]。



Boost还实现了标准C ++ 11例如,库,以及Apache。请参阅上面的第二个链接以获取更多信息。



-SA
C++ does not have any threading facilities embedded in the language. It relies on some API or library implementing threading. It could be a particular OS API, or the library implementing some OS abstraction layer, that is, applicable to many OS. Here, you have some choice. One of the library is special because it is standardized. This is the C++ Standard Library:
http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library[^],
http://en.wikipedia.org/wiki/C%2B%2B_Standard_Library#Thread_support_library[^].

Note that threading types have been introduced only with C++11: http://en.wikipedia.org/wiki/C%2B%2B11[^].

You can find some code samples in many places. For example: http://stackoverflow.com/questions/266168/simple-example-of-threading-in-c[^].

If you use anything older than that, you have many options: OS-specific APIs (I would advise you always tag your platform(s) in you questions), and many alternatives, such as boost:
http://en.wikipedia.org/wiki/Boost_library[^],
http://www.boost.org[^].

Boost also has its implementation of standard C++11 library, as well as Apache, for example. See the second link above for further information.

—SA


这篇关于c ++中的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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