[英]How do I thread a function?
初學者的問題,但是我該如何穿線?
我有以下代碼片段:
std::vector<std::thread*> threads[8];
for (unsigned short rowIndex = 0; rowIndex < unimportantStuff.rows; ++rowIndex)
{
for (unsigned short columnIndex = 0; columnIndex < unimportantStuff.columns; ++columnIndex)
{
myModelInstance = new CModelInstance;
myModelInstance->Init(myLoader.CreateTriangle(myFramework.myDevice, { -0.8f + unimportantStuff.offset*columnIndex, -0.8f + unimportantStuff.offset*rowIndex }), { -0.8f + unimportantStuff.offset*columnIndex, -0.8f + unimportantStuff.offset*rowIndex });
myScene.AddModelInstance(myModelInstance);
}
}
如果可能,我想同時使用Init函數和AddModelInstance函數,但是我不知道如何繼續。 如何激活多個線程(在這種情況下,最多可以激活8個線程)?
我試過這樣的單線程:
std::thread t1(myScene.AddModelInstance, myModelInstance);
但是我收到以下錯誤:
CScene :: AddModelInstance':非標准語法; 使用“&”創建指向成員的指針
我嘗試在函數和參數中都添加&,但是都沒有用。
代替這個:
std::thread t1(myScene.AddModelInstance, myModelInstance);
您需要這樣的東西:
std::thread t1(&Scene::AddModelInstance, myScene, myModelInstance);
&Scene::AddModelInstance
是指向您要調用的成員函數的指針,該函數可能隱含this
參數( myScene
)。
假設myScene
屬於Scene
類型,請嘗試以下操作:
std::thread t1(&Scene::AddModelInstance, &myScene, myModelInstance);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.