한 번에 N 개의 스레드를 만들고 각 스레드에서 함수를 실행하는 일부 C# 코드를 변환하려고합니다.스로틀 C++ 스레드
나는이 두 가지 문제 : 나는 한 번에 N 스레드를 제한 할 - 어떻게
?
-My 링커는 정적 메서드 인 FastestMemory와 SlowestMemory를 인식하고있는 것처럼 보입니다. (필자는 값을 끝까지 출력 할 때)
누군가 도와주세요.
지금까지 내가 가진 :
#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;
}
안녕하세요, Jerry, 예, 10 개의 작업을 실행하는 5 개의 스레드를 의미합니다 (따라서 실행중인 스레드는 대략 두 번 반복됩니다). – mezamorphic
@ user1107474 :이 경우 스레드 풀이 필요합니다. 기본 아이디어는 [thread-safe queue]를 만드는 것입니다 (http://stackoverflow.com/questions/2375132/releasesemaphore-does-not-release-the-semaphore/2375370#2375370). 'thread_data'를 메인의 대기열에 넣으십시오. 스레드 함수는 반복적으로 대기열에서 항목을 검색하고 처리 한 다음 다른 항목을 가져옵니다 (해당 질문에는 일반적인 아이디어를 보여주는 샘플 코드가 있음). –