2012-04-02 4 views
0

한 번에 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; 
    } 

답변

1

난 당신이 "한 번에 제한 N 스레드"에 대해 무슨 뜻인지 모르겠어요. 질문에 10 개의 작업을 실행하기 위해 5 개의 스레드 만 사용 (예를 들어)하고 싶습니까?

그렇다면 어떤 종류의 스레드 풀을 사용할 수 있습니다. Windows에는 I/O 완료 포트와 함께 3 개의 개별 스레드 풀 API가 있으며 스레드 풀로도 작동합니다. 부족한 것을 발견하면 자신의 스레드 풀을 작성하는 것도 꽤 쉽습니다. 그러나 구조는 게시 한 것과 상당히 다릅니다.

static unsigned int FastestMemory;을 선언하지만, 변수를 정의하지 않습니다.

class Test { 
    static unsigned int FastestMemory; 
    static unsigned int SlowestMemory; 
    // ... 
}; 

unsigned int Test::FastestMemory = 0; 
unsigned int Test::SlowestMemory = 0; 
+0

안녕하세요, Jerry, 예, 10 개의 작업을 실행하는 5 개의 스레드를 의미합니다 (따라서 실행중인 스레드는 대략 두 번 반복됩니다). – mezamorphic

+0

@ user1107474 :이 경우 스레드 풀이 필요합니다. 기본 아이디어는 [thread-safe queue]를 만드는 것입니다 (http://stackoverflow.com/questions/2375132/releasesemaphore-does-not-release-the-semaphore/2375370#2375370). 'thread_data'를 메인의 대기열에 넣으십시오. 스레드 함수는 반복적으로 대기열에서 항목을 검색하고 처리 한 다음 다른 항목을 가져옵니다 (해당 질문에는 일반적인 아이디어를 보여주는 샘플 코드가 있음). –

0

정적 int는 선언하고 있지만 정의하지는 않습니다.

시도 :

class Test{ 

public: 
    static unsigned int FastestMemory; 
    static unsigned int SlowestMemory; 
    // ... 
}; 

unsigned int Test::FastestMemory = 0; 
unsigned int Test::SlowestMemory = 0; 

지금까지 무엇을보고, N 스레드를 정의에 관해서는, 개별 스레드를 생성하고 필요에 따라 확장 할 CreateThread를 사용해보십시오. 물론이 작업을 수행하면 아주 기본적인 스레드 모델이 될 수 있습니다. 스레드 풀에서 읽는 것이 좋습니다.

+0

그는 실제로 선언하고 있지만 정의하지는 않습니다. –

+0

@Konrad, CreateThread-를 사용하고 있습니다. 그러나 처음 스레드가 완료 될 때까지 다음 스레드 그룹을 어떻게 지연시켜야할지 모르겠습니다. – mezamorphic

+0

좋은 장소, 긴 하루였습니다 ;-) – Konrad