1

我正在实例化创建线程的类的多个实例。既然线程是静态的,那么对象的实例会不会干扰彼此的线程操作呢?

奇怪的是,我看到 2 个不同的波形并以两种不同的睡眠延迟运行。???那么这是否意味着有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 ???
4

1 回答 1

0

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

于 2012-04-20T06:36:26.693 回答