CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009

    how use multithread with a loop?

    how can i add these 'for' on a multithread?
    for(int X=100, H=imgTexture.ImageHeight; X<1000; X++)        {
                if(Speed >1000) Speed=1;
                img.DrawLine3D({(float)X-Speed,100,(float)H-Speed},{(float)X-Speed,400,(float)H-Speed},0,imgTexture, H,0,false);
    these loop depends on 'img' and 'imgTexture' and 'Speed'.
    i can create a function void with these loop, but i don't understand how can i add it on multithread

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Hanover Germany

    Re: how use multithread with a loop?

    Why do you think you need a "multithread"?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    Re: how use multithread with a loop?

    The point that sticks out to me is why is the if statement within the loop as Speed doesn't change within the loop.

    if(Speed >1000) Speed=1;
    for (int X = 100, H = imgTexture.ImageHeight; X < 1000; ++X, ++H)
        img.DrawLine3D({(float)X - Speed, 100, (float)H-Speed}, {(float)X - Speed, 400, (float)H - Speed}, 0, imgTexture, H, 0, false);
    If you need to convert this into multi-threaded, then consider turning it into a for-each with an execution policy and a function for the img.DrawLIne3D

    You might want to consider having say 10 for-each iterations with the draw function being a loop.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.5.3)

  4. #4
    Join Date
    Apr 2009

    Re: how use multithread with a loop?

    so true... i did a several questions on some forums. so sorry something..(some forums need more questions, they think that i can ask off-topic, but the problem is the same )
    but the big problem is: how can i speed up my algorithm?
    i lose more speed on loops. but heres a code with multitthread did by me:
    #include <iostream>#include <thread>
    #include <windows.h>
    #include <math.h>
    using namespace std;
    class image
        int ImageWidth = 0;
        int ImageHeight = 0;
        HDC ImageHDC = NULL;
        HBITMAP ImageBitmap;
        HBITMAP oldBit;
        BITMAP bmp;
        BITMAPINFO info;
        size_t pixelSize;
        size_t scanlineSize;
        size_t bitmapSize;
        void* p;
        LPBYTE Pixels;
        void Clear(COLORREF BackColor = RGB(0,0,0))
            RECT rec{0,0,ImageWidth,ImageHeight};
            HBRUSH HB = CreateSolidBrush(BackColor);
        image(int Width, int Height, COLORREF BackColor=RGB(0,0,0))
            ImageHDC = CreateCompatibleDC(NULL);
            ImageWidth = Width;
            ImageHeight =Height;
            ZeroMemory (&info, sizeof (BITMAPINFO));
            info.bmiHeader.biSize = sizeof(info.bmiHeader);
            info.bmiHeader.biWidth = ImageWidth;
            // pay attention to the sign, you most likely want a
            // top-down pixel array as it's easier to use
            info.bmiHeader.biHeight = -ImageHeight;
            info.bmiHeader.biPlanes = 1;
            info.bmiHeader.biBitCount = 32;
            info.bmiHeader.biCompression = BI_RGB;
            info.bmiHeader.biSizeImage = 0;
            info.bmiHeader.biXPelsPerMeter = 0;
            info.bmiHeader.biYPelsPerMeter = 0;
            info.bmiHeader.biClrUsed = 0;
            info.bmiHeader.biClrImportant = 0;
            // the following calculations work for 16/24/32 bits bitmaps
            // but assume a byte pixel array
            ImageBitmap = CreateDIBSection(ImageHDC, &info, DIB_RGB_COLORS, (LPVOID*)&Pixels, 0, 0);
            if(ImageBitmap ==NULL) cout << "no HBITMAP";
            if(SelectObject(ImageHDC, ImageBitmap)==NULL) cout << "error";
            pixelSize = info.bmiHeader.biBitCount / 8;
            // the + 3 ) & ~3 part is there to ensure that each
            // scan line is 4 byte aligned
            scanlineSize = (pixelSize * info.bmiHeader.biWidth + 3) & ~3;
            bitmapSize = bmp.bmHeight * scanlineSize;
        void NewSetPixel(HDC DestinationHDC, int X, int Y, BYTE RedColor, BYTE GreenColor, BYTE BlueColor)
            size_t pixelOffset = (Y) *scanlineSize + (X) *pixelSize;
        void DrawLine( float X0, float Y0, float Z0, float X1, float Y1, float Z1, COLORREF LineColor)
            //Getting Line Distance(float results) coordenates and line:
            float DX = abs(X1 - X0);
            float DY = abs(Y1 - Y0);
            float DZ = abs(Z1 - Z0);
            float LineDistance =sqrt((DX * DX) + (DY * DY) + (DZ * DZ));
            //Getting the Steps incrementation(float results):
            float XSteps = DX/LineDistance;
            float YSteps = DY/LineDistance;
            float ZSteps = DZ/LineDistance;
            //Draw Line using the Steps\ Incrementation:
            float X = X0;
            float Y = Y0;
            float Z = Z0;
            BYTE R = GetRValue(LineColor);
            BYTE G = GetGValue(LineColor);
            BYTE B = GetBValue(LineColor);
            std::thread th[(int)LineDistance];
            int PosX;
            int PosY;
            for(int i =0; i <LineDistance; i++)
                //For every steps we calculate the perspective:
                float EyeDistance = 500;
                //Avoiding division by zero:
                if(Z==0) Z=1;
                float Perspective = EyeDistance/(EyeDistance+Z);
                //The 3D to 2D convertion(i use 300 of eye distance, but we can change it):
                PosX = trunc(X*Perspective);
                PosY = trunc(Y*Perspective);
                if(Z>=0 && PosX<ImageWidth && PosX>=0 && PosY<ImageHeight && PosY>=0)
                    th[i] = std::thread(&image::NewSetPixel,this,ImageHDC, PosX,PosY,R,G,B);
                    //NewSetPixel(ImageHDC, PosX,PosY,R,G,B); //more speed than my multithread
                //Increment steps(integer results):
        void DrawRectangle(float PosX, float PosY, float PosZ, float Width, float Height, float Depth, COLORREF Color = RGB(255,0,0), bool Filled = false)
            DrawLine( PosX, PosY, PosZ,PosX + Width, PosY, PosZ + Depth, Color);
            DrawLine( PosX, PosY, PosZ, PosX, PosY + Height, PosZ, Color);
            DrawLine( PosX + Width, PosY, PosZ + Depth, PosX + Width, PosY+Height, PosZ + Depth, Color);
            DrawLine( PosX, PosY + Height, PosZ, PosX + Width, PosY + Height, PosZ + Depth, Color);
                for(int i = 0; i<Height; i++)
                    DrawLine( PosX, PosY + i, PosZ,PosX + Width, PosY +i, PosZ + Depth, Color);
            SelectObject(ImageHDC, oldBit);
    image img(400,600);
    int main()
        int FramePerSecond=0;
        int Frames=0;
        RECT rec2 ={0,30,170,50};
        auto Start =GetTickCount();
        float PosX=0;
        float PosY =50;
        float PosZ =0;
            if(GetAsyncKeyState(VK_UP) & 0x8000)
            img.DrawRectangle(0,50,PosZ, 500,400,1000, RGB(255,0,0),true);
            string s="Frame Per Second: " + to_string(FramePerSecond);
            DrawText(img.ImageHDC,s.c_str(),-1, &rec2, DT_CENTER|DT_VCENTER|DT_SINGLELINE);
            auto Actual =GetTickCount();
            if((Actual-Start) >= 1000)
                Start = GetTickCount();
                FramePerSecond =Frames;
        }while(!(GetAsyncKeyState(VK_ESCAPE) & 0x8000));
        return 0;
    now see the problem on DrawLine() and NewSetPixel()... they loose too much speed.
    "Process returned 0 (0x0) execution time : 82.786 sPress any key to continue."
    i learned the C\C++, by me: tutorials, books and forums and experience... what i mean is: we can make algorithms, but we don't learn how we can speed up... only learn, for exemple, the SetPixel() is much more slow than DIB's... but the loops and array's can be slow too... and i don't know how win speed. we can learn Multithread... but, like you see, is more slow.
    without multithread, the same code, i get:
    "Process returned 0 (0x0) execution time : 0.109 sPress any key to continue."
    but with 40FPS.. i only draw 1 rectangle with Z. imagine 10 rectangles... it will be too much slow.
    i don't know how can i win more speed
    Last edited by Cambalinho; November 30th, 2022 at 04:54 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width