实例化创建线程的类的多个实例......?

时间:2012-04-20 04:57:37

标签: c++ multithreading visual-c++

我正在实例化创建线程的类的多个实例。由于线程是静态的,对象的实例是否会干扰彼此的线程操作?

奇怪的是,我看到了两种不同的波形,并在两种不同的睡眠延迟下运行。 ???这是否意味着有2个不同的线程???

void CWaveGeneration::CreateWave()
{

    Y = new double[numPoints];
    X = new double[numPoints];
    I = new int[numPoints];


    CWaveGeneration *pp = this;
    hThread_Wave = CreateThread(NULL, 0, Thread_Wave, pp, 0, NULL); 
    //within the thread, there is the setting of Sleep(iSleep);
}


void CWaveGeneration::CreateWave(int _waveType, double _A, double _w, double _T, double _r, int _numPoints, int _iSleep)
{
    waveType = _waveType;
    A = _A;
    w = _w;
    T = _T;
    r = _r;
    numPoints = _numPoints;
    iSleep = _iSleep;

    CreateWave();
}


DWORD WINAPI CWaveGeneration::Thread_Wave(LPVOID iValue)
{
    CWaveGeneration *p = (CWaveGeneration*)iValue;
    switch (p->waveType)
    {
    case 0:
        p->Sine();
        break;
    case 1:
        p->Square();
        break;
    case 2:
    //  p->Triangle();
        break;
    case 3:
    //  p->SawTooth();
        break;
    }

    return true;
}

来自头文件:

static DWORD WINAPI Thread_Wave(LPVOID iValue);

    wave1 = new CWaveGeneration();
    wave1->CreateWave(0,100,10,0,0,200, 10);

    wave2 = new CWaveGeneration();
    wave2->CreateWave(1,80,5,0,0,200, 100);

    // in total, are there 1 thread or 2 threads created here ???

1 个答案:

答案 0 :(得分:0)

如果您的类中的线程对象是静态的,那么您将只有一个线程用于该类的所有实例 如果类中的线程对象不是静态的,那么该类的每个实例都将有1个线程对象。这应该没有任何问题。