C++中的多线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4768294/
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
Multithreading in c++
提问by gemexas
I want to run one function with different parameters on different threads:
我想在不同的线程上运行一个具有不同参数的函数:
int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
for (int i=0; i<threads; i++){
//new_thread function(par1[i], par2[i]);
}
I know nothing about threads. I tried to do something the windows API (can't use other libraries), but it doesn't work. How should I implement this? And it is possible to start an unknown number of threads of the time of programming (dynamically creating threads)?
我对线程一无所知。我试图对 windows API 做一些事情(不能使用其他库),但它不起作用。我应该如何实施?并且可以在编程时启动未知数量的线程(动态创建线程)?
回答by Nawaz
One sample example for Windows,
Windows 的一个示例示例,
#include <Windows.h>
struct thread_data
{
int m_id;
thread_data(int id) : m_id(id) {}
};
DWORD WINAPI thread_func(LPVOID lpParameter)
{
thread_data *td = (thread_data*)lpParameter;
cout << "thread with id = " << td->m_id << endl;
return 0;
}
int main()
{
for (int i=0; i< 10; i++)
{
CreateThread(NULL, 0, thread_func, new thread_data(i) , 0, 0);
}
}
In this example, each thread gets different data, namely of type thread_data
, which is passed as argument to the thread function thread_func()
.
在这个例子中,每个线程获得不同的数据,即类型thread_data
,作为参数传递给线程函数thread_func()
。
Read these to know how to create thread on windows:
阅读这些以了解如何在 Windows 上创建线程:
http://msdn.microsoft.com/en-us/library/ms682516(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms682453(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/ms682516(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms682453(v=vs.85).aspx
Also, you may like this suggestion as well:
此外,您可能也喜欢这个建议:
回答by Konrad Rudolph
I know nothing about threads.
我对线程一无所知。
This is a problem. Threads is one of the area where experimenting without at least sometheoretical background usually ends in a catastrophe. There's so much that can go wrong with threading, but that doesn't occur until much, muchlater.
这是个问题。线程是在没有至少一些理论背景的情况下进行实验通常以灾难告终的领域之一。线程可能会出错的地方太多了,但是直到很久以后才会出现这种情况。
I would suggest either getting a book about multithreading or reading a few articles online before starting.
我建议在开始之前要么买一本关于多线程的书,要么在网上阅读几篇文章。
If you are interested in using multithreading for for efficiency then Herb Sutter's columnmay be for you. And then there's the excellent Introduction to Parallel Computingby Blaise Barney.
如果您对使用多线程来提高效率感兴趣,那么Herb Sutter 的专栏可能适合您。然后是 Blaise Barney出色的并行计算简介。
回答by Starkey
You need to do a couple of things to get this to work. First of all the windows thread function takes this signature:
您需要做几件事才能使其发挥作用。首先,windows 线程函数采用这个签名:
DWORD WINAPI ThreadFunction(LPVOID args)
The thread startup routine is this function:
线程启动例程是这个函数:
HANDLE WINAPI CreateThread(
__in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes,
__in SIZE_T dwStackSize,
__in LPTHREAD_START_ROUTINE lpStartAddress,
__in_opt LPVOID lpParameter,
__in DWORD dwCreationFlags,
__out_opt LPDWORD lpThreadId
);
Note that the way to pass arguments to your thread function is through an LPVOID. That means you need to create a structure to hold your par1
and par2
. You would then pass in a pointer to this structure and the pull the contents out in your thread routine.
请注意,将参数传递给线程函数的方式是通过 LPVOID。这意味着您需要创建一个结构来保存您的par1
和par2
。然后您将传入一个指向该结构的指针,并在您的线程例程中提取内容。
回答by Flexo
Since OpenMPis supported in Visual studiowhy not use that? It's far simpler than managing your own threads manually, is very portable and the example you gave is almost perfect for it. Wikipedia has a reasonble introduction to the concepts of OpenMP.
由于OpenMP的是在Visual Studio支持为什么不利用呢?它比手动管理您自己的线程要简单得多,而且非常便携,而且您提供的示例几乎非常适合它。维基百科对 OpenMP 的概念进行了合理的介绍。
In your example the simple (and slightly naive - normally you don't explicitly say the number of threads, you say the number of work units) code is:
在您的示例中,简单(而且有点天真 - 通常您不会明确说明线程数,而是说工作单元数)代码是:
int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
#pragma omp parallel for
for (int i=0; i<threads; i++){
function(par1[i], par2[i]);
}
With an appropriate option passed to the compiler to enable OpenMP. Without this option it still compiles and runs as a serial program too.
将适当的选项传递给编译器以启用 OpenMP。如果没有这个选项,它仍然作为串行程序编译和运行。
回答by Beno?t
May i suggest the use of boost::thread_group?
我可以建议使用boost::thread_group吗?
boost::thread_group g;
int threads = 3;
int par1[] = {1, 2, 3};
int par2[] = {4, 5, 6};
for (int i=0; i<threads; i++){
g.create_thread(/*function*/);
//new_thread function(par1[i], par2[i]);
}
g.join_all();
回答by Poni
You create a thread in Windows by calling CreateThread. There's an example here.
您可以通过调用CreateThread在 Windows 中创建一个线程。这里有一个例子在这里。
Yes, you can dynamically create threads.
是的,您可以动态创建线程。
Now, when your "thread function" is getting called (which means the thread started) you use the passed parameter which you've feeded when calling CreateThread.
At the "thread-function" it's "LPVOID lpParam". You should cast it to your type. If it's two objects you need to pass to the thread then create your own custom struct/class with the appropriate memebers, and pass an instance of it when calling CreateThread so you can use it later. Very straight-forward.
现在,当您的“线程函数”被调用(这意味着线程已启动)时,您将使用您在调用 CreateThread 时提供的传递参数。
在“线程函数”中,它是“LPVOID lpParam”。您应该将其转换为您的类型。如果它是两个对象,您需要传递给线程,然后使用适当的成员创建自己的自定义结构/类,并在调用 CreateThread 时传递它的一个实例,以便您以后可以使用它。非常直接。