gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 14:17:15 25 4
gpt4 key购买 nike

我正在实例化创建线程的类的多个实例。既然线程是静态的,对象的实例会不会互相干扰线程运行呢?

奇怪的是,我看到了 2 种不同的波形,并以两种不同的 sleep 延迟运行。 ???那么这是否意味着有 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个线程对象。这应该没有问题。

关于c++ - 实例化创建线程的类的多个实例......?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10240499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com