2012-04-02 22 views
0

我想翻译一些C#代码,它一次创建N个线程,并在每个线程中运行一个函数。节流C++线程

我有两个问题:

- 我该如何限制N个线程?

- 当我在主要方法中引用它们时(当我在最后打印出值时),我的链接程序似乎无法识别静态整数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个线程”是什么。你的意思是你想要(例如)只使用5个线程来执行你的问题中的10个任务吗?

如果是这样,你可能想要使用某种类型的线程池。 Windows有三个独立的线程池API和I/O完成端口,它们也可以充当线程池。如果你发现它们缺乏,编写自己的线程池也很容易 - 但是结构与你发布的结构有很大不同。

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

嗨杰里,是的,我的意思是5个线程执行10个任务(因此大致执行两次线程迭代)。 – mezamorphic 2012-04-02 16:37:10

+0

@ user1107474:在这种情况下,您需要一个线程池。基本思想是创建一个[线程安全队列](http://stackoverflow.com/questions/2375132/releasesemaphore-does-not-release-the-semaphore/2375370#2375370)。将你的'thread_data'放入main中的队列中。你的线程函数重复从队列中检索一个项目,处理它,然后检索另一个(该问题有一些示例代码显示了总体思路)。 – 2012-04-02 16:41:25

0

您声明但未定义静态整数。

尝试:

class Test{ 

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

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

用于定义N个线程,看你有什么到目前为止,尝试使用CreateThread创建单独的线程,并根据需要扩展。当然,这样做会给你一个非常基本的,可能不是很有用的线程模型。我建议阅读线程池。

+0

他真的宣布但没有定义。 – 2012-04-02 16:35:39

+0

@Konrad,我正在使用CreateThread-但我不确定如何延迟下一组线程运行,直到第一个线程完成? – mezamorphic 2012-04-02 16:38:24

+0

不错的地方,这是一个漫长的一天;-) – Konrad 2012-04-02 16:39:05