C++ 节流C++;线程
我试图翻译一些C代码,它一次创建N个线程,并在每个线程中运行一个函数 我有两个问题: -如何一次限制N个线程 -当我在main方法中引用静态ints FastestMemory和SlowestMemory时(当我最后打印出值时),我的链接器似乎无法识别它们 有人能帮忙吗 到目前为止,我已经:C++ 节流C++;线程,c++,visual-c++,C++,Visual C++,我试图翻译一些C代码,它一次创建N个线程,并在每个线程中运行一个函数 我有两个问题: -如何一次限制N个线程 -当我在main方法中引用静态ints FastestMemory和SlowestMemory时(当我最后打印出值时),我的链接器似乎无法识别它们 有人能帮忙吗 到目前为止,我已经: #include "stdafx.h" #include <Windows.h> #include <iostream> #include <vector> #inclu
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
class Test{
public:
static unsigned int FastestMemory;
static unsigned int SlowestMemory;
public:
Test(unsigned a, unsigned b){
FastestMemory = a;
SlowestMemory = b;
}
struct thread_data
{
int m_id;
thread_data(int id) : m_id(id) {}
};
static DWORD WINAPI thread_func(LPVOID lpParameter)
{
thread_data *td = (thread_data*)lpParameter;
int RepetitionNumber = td->m_id;
printf("thread with id = " + RepetitionNumber + '\n');
unsigned int start = clock();
vector<byte> list1;
vector<byte> list2;
vector<byte> list3;
for(int i=0; i<10000000; i++){
list1.push_back(57);
}
for (int i = 0; i < 20000000; i=i+2)
{
list2.push_back(56);
}
for (int i = 0; i < 10000000; i++)
{
byte temp = list1[i];
byte temp2 = list2[i];
list3.push_back(temp);
list2[i] = temp;
list1[i] = temp2;
}
unsigned int timetaken = clock()-start;
printf(RepetitionNumber + " Time taken in millisecs: " + timetaken);
if(timetaken < FastestMemory){
FastestMemory = timetaken;
}
if(timetaken > SlowestMemory){
SlowestMemory = timetaken;
}
return 0;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Test* t = new Test(2000000,0);
for (int i=0; i< 10; i++)
{
CreateThread(NULL, 0, Test::thread_func, new Test::thread_data(i) , 0, 0);
}
printf("Fastest iteration:" + Test::FastestMemory + '\n'); //Linker not recognising
printf("Slowest iteration:" + Test::SlowestMemory + '\n'); //Linker not recognising
int a;
cin >> a;
}
#包括“stdafx.h”
#包括
#包括
#包括
#包括
使用名称空间std;
课堂测试{
公众:
静态无符号整数最快存储器;
静态无符号内存;
公众:
测试(无符号a、无符号b){
最快记忆=a;
最慢存储器=b;
}
结构线程数据
{
国际货币基金组织;
线程数据(int-id):m\u-id(id){}
};
静态DWORD WINAPI线程函数(LPVOID lpParameter)
{
thread_data*td=(thread_data*)lp参数;
int RepetitionNumber=td->m_id;
printf(“id为=“+RepetitionNumber+”\n')的线程;
无符号整数开始=时钟();
向量列表1;
向量列表2;
向量列表3;
for(int i=0;i最慢内存){
最慢内存=所用时间;
}
返回0;
}
};
int _tmain(int argc,_TCHAR*argv[]
{
试验*t=新试验(2000000,0);
对于(int i=0;i<10;i++)
{
CreateThread(NULL,0,Test::thread\u func,new Test::thread\u data(i),0,0);
}
printf(“最快迭代:“+Test::FastestMemory+'\n”);//链接器无法识别
printf(“最慢迭代:+Test::SlowestMemory+'\n');//链接器无法识别
INTA;
cin>>a;
}
我不知道你说的“一次限制N个线程”是什么意思。您的意思是(例如)只想使用5个线程来执行问题中的10项任务吗
如果是这样,您可能需要使用某种线程池。Windows有三个独立的线程池API,以及I/O完成端口,它们也可以充当线程池。如果你发现自己的线程池不存在的话,那么编写一个线程池也很容易——但是它的结构与你发布的有点不同
静态无符号int最快内存代码>声明但不定义变量。您需要在类定义之外定义它:
class Test {
static unsigned int FastestMemory;
static unsigned int SlowestMemory;
// ...
};
unsigned int Test::FastestMemory = 0;
unsigned int Test::SlowestMemory = 0;
您正在声明但未定义静态int
尝试:
至于定义N个线程,请查看到目前为止的情况,尝试使用创建单个线程,并根据需要进行扩展。当然,这样做将为您提供一个非常基本但可能不是非常有用的线程模型。我建议阅读线程池。他实际上是在声明,但不是在定义。@Konrad,我正在使用CreateThread-但我不确定如何延迟下一组线程的运行,直到第一组线程完成为止?很好,这是漫长的一天;-)嗨,Jerry,是的,我的意思是5个线程执行10个任务(因此大约有两个线程执行的迭代)。@user1107474:在这种情况下,您需要一个线程池。基本想法是创建一个新的应用程序。将线程\u数据
放入main中的队列。您的线程函数反复从队列中检索一个项目,对其进行处理,然后检索另一个项目(该问题有一些示例代码显示了一般思路)。
class Test{
public:
static unsigned int FastestMemory;
static unsigned int SlowestMemory;
// ...
};
unsigned int Test::FastestMemory = 0;
unsigned int Test::SlowestMemory = 0;