Multithreading problem

HI,
i’m developing freehand drawing game, and in order to increase performance, i would use multithreading , i used this code:

void Canvas::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event)  {
   auto t = std::thread(&Canvas::drawEvenlySpacedSprites, this,touch->getLocation(),touch->getPreviousLocation());
    t.detach();
}


void  Canvas::drawEvenlySpacedSprites(Vec2 start, Vec2 end) {
   // begin drawing to the render texture
    _target->begin();
    
     float distance = start.getDistance(end);
   if (distance > 1) {
    int d = (int)distance;

        for (int i = 0; i < d; i++)
        {
        float difx = end.x - start.x;
        float dify = end.y - start.y;
        float delta = (float)i / distance;

        Sprite * sprite = Sprite::create("brush3.png");
        sprite->setColor(Color3B::BLUE);
        sprite->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));
        sprite->visit();
       
      }
     }

// finish drawing and return context back to the screen
_target->end();


  */
}

But I have this error :

libpng warning: iCCP: known incorrect sRGB profile
cocos2d: Assert failed: Cannot add command while rendering
Assertion failed: (!_isRendering), function addCommand, file         /Users/XXXXX/FreeDrawing/cocos2d/cocos/renderer/CCRenderer.cpp, line 268.

if i use t.join(); instead of t.detach(); , it work! , but it change the brush scale and the draw isn’t smooth
Help me please .
Thanks

Any help please, i really need it :frowning:

cocos2d-x is not multithreading safe.
when you call visit(), v3.x will add a command to render queue.

Thanks, but is there any solution to do multithreading ?

I think you should use a cpu algorithm, not use sprite render many times.

I don’t understand you, please give me more details

I believe that pushing rendering commands into separate threads won’t give you any performance gain. CPU multithreading would be effective if you’d use it for matrix calculations and similar tasks.

Thanks, but why i can’t use global sprite in this section( i can in cocos2d-x v2.x)

Sprite * sprite = Sprite::create("brush3.png");
        sprite->setColor(Color3B::BLUE);
        sprite->setPosition(Vec2(start.x + (difx * delta), start.y + (dify * delta)));
        sprite->visit();

Please tell me how can i , improve this code