Jump to content
  • Advertisement
Sign in to follow this  
cambalinho

why that i get a signal SIGSEGV with my Multithread code?

This topic is 817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

heres my multithread class:

class Multithread
{
private:
    HANDLE hThread=NULL;
    DWORD dwThreadId=0;
    bool blnSingleThreadAtTime=false;
    bool blnCreationOrder=true;
    bool blnWait=false;
    function<void()> multithreadfunction=nullptr;
    function<void(LPVOID param)> multithreadparameterfunction=nullptr;
    HANDLE myEvent = CreateEvent(0, 0, 0, 0);
    LPVOID parameter=NULL;

    struct mytread
    {
        Multithread *classpointer=NULL;
        LPVOID multithreadparameters=NULL;
    };
    vector<mytread> mtparameterthread;

public:

    void SingleThreadAtTime(bool SingleThread)
    {
        blnSingleThreadAtTime=SingleThread;
    }

    bool SingleThreadAtTime()
    {
        return blnSingleThreadAtTime;
    }

    //make the threads by creation order
    void CreationOrder(bool OrderCreation)
    {
        blnCreationOrder=OrderCreation;
    }

    bool CreationOrder()
    {
        return blnCreationOrder;
    }

    void WaitCondition()
    {
        WaitForSingleObject(myEvent, INFINITE);
        blnWait=true;
    }

    void SetCondition()
    {
        SetEvent(myEvent);
        blnWait=false;
    }

    static DWORD WINAPI MyThreadFunction( LPVOID lpParam )
    {
        static bool blnfirsttime=false;
        Multithread *pThis = static_cast<Multithread*>(lpParam);
        if(pThis->blnSingleThreadAtTime==true && blnfirsttime==true)
            pThis->WaitCondition();
        blnfirsttime=true;
        if(pThis->multithreadfunction)
            pThis->multithreadfunction();
        if(pThis->blnSingleThreadAtTime==true && blnfirsttime==true)
            pThis->SetCondition();

        return 0;
    }

    static DWORD WINAPI MyParameterThreadFunction( LPVOID lpParam )
    {
        static bool blnfirsttime=false;
        mytread *pThis = static_cast<mytread*>(lpParam);
        if(pThis->classpointer->blnSingleThreadAtTime==true && blnfirsttime==true)
            pThis->classpointer->WaitCondition();
        blnfirsttime=true;
        if(pThis->classpointer->multithreadparameterfunction)
            pThis->classpointer->multithreadparameterfunction(pThis->multithreadparameters);
        if(pThis->classpointer->blnSingleThreadAtTime==true && blnfirsttime==true)
            pThis->classpointer->SetCondition();
        return 0;
    }

    Multithread(std::function<void()> SetFunction)
    {
        mtparameterthread.reserve(100);
        multithreadfunction=SetFunction;
    }

    Multithread( function<void(LPVOID)> SetFunction )
    {
        mtparameterthread.reserve(100);
        multithreadparameterfunction =SetFunction;
    }

    //corrigir
    template<typename tpVariant>
    Multithread( function<void(tpVariant)> SetFunction )
    {
        mtparameterthread.reserve(100);
        multithreadparameterfunction = [=]( LPVOID pv ) { SetFunction( static_cast<tpVariant>( pv ) ); };
    }

    int i=-1;
    template<typename tpVariant>
    void operator ()(tpVariant &vrParam)
    {
        //LPVOID lpParam=static_cast<LPVOID>(&vrParam);
        i=i+1;
        mtparameterthread[i].classpointer = this;
        mtparameterthread[i].multithreadparameters = &vrParam;

        hThread= CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size
            &Multithread::MyParameterThreadFunction,       // thread function name
            &mtparameterthread[i],          // argument to thread function
            0,                      // use default creation flags
            &dwThreadId);
        if(i==100)
            i=-1;
        if(blnCreationOrder==true)
            delay(100);
    }

    void operator ()(LPVOID vrParam)
    {
        i=i+1;
        mtparameterthread[i].classpointer = this;
        mtparameterthread[i].multithreadparameters = vrParam;

        hThread= CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size
            &Multithread::MyParameterThreadFunction,       // thread function name
            &mtparameterthread[i],          // argument to thread function
            0,                      // use default creation flags
            &dwThreadId);
        if(i==100)
            i=-1;
        //delay for avoiding execution of all threads in same time and for not losing data because of that
        if(blnCreationOrder==true)
            delay(100);

    }

    void operator ()()
    {
       hThread  = CreateThread(
            NULL,                   // default security attributes
            0,                      // use default stack size
            &Multithread::MyThreadFunction,       // thread function name
            this,          // argument to thread function
            0,                      // use default creation flags
            &dwThreadId);
        if(blnCreationOrder==true)
            delay(100);
    }

    ~Multithread()
    {
        SetCondition();
        if(myEvent)
            CloseHandle(myEvent);
        if(hThread)
            CloseHandle(hThread);
    }
};

i'm trying use it on my ConsoleWindow:

class consolewindow
{
    HWND consoleedit=NULL;
    form frmconsolewindow{"console"};
    COLORREF clrTextColor=RGB(255,255,255);
    COLORREF clrTextBackColor=RGB(0,0,0);
    protected: bool blnread=false;
    protected: string readstring="";
    Timer tmrCreate{Event<>([&](){Create();})};

    struct strucread
    {
        LPVOID varadress=NULL;
        string vartype="";
        variant varvalue="";
    };
    vector<strucread> readdata;

    //these constructor function works fine for a string, but not for a numbers or char* :(
     Multithread mtRead{ [&]( LPVOID varname)
    {

        strucread *strTest=static_cast<strucread*>(varname);
        if(blnread==true)
            mtRead.WaitCondition();
        if(strTest==NULL)
            return;
        if(strTest->vartype!="write")// && strTest->vartype!="write: string" && strTest->vartype!="write: double" && strTest->vartype!="write: float" && strTest->vartype!="write: char*" && strTest->vartype!="write: const char*" && strTest->vartype!="write: char" && strTest->vartype!="write: const char")
        {
            blnread=true;
        }


        if(strTest->vartype=="string")
            *((string*)(strTest->varadress)) = readstring;
        else if(strTest->vartype=="char*")
        {
            char *cstr = new char[readstring.length() + 1];
            strcpy(cstr, readstring.c_str());
            *((char*)(strTest->varadress)) =cstr[0];
            delete [] cstr;
        }
        else if(strTest->vartype=="char")
        {
            char cstr = readstring[0];
            *((char*)(strTest->varadress)) =cstr;
        }
        else if(strTest->vartype=="double")
        {
            if(is_number(readstring)==true)
            {
                *((double*)(strTest->varadress)) = stof(readstring);
            }
            else
            {
                *((double*)(strTest->varadress)) =0;
            }
        }
        else if(strTest->vartype=="int")
        {
            if(is_number(readstring)==true)
            {
                *((int*)(strTest->varadress)) = stoi(readstring);
            }
            else
            {
                *((int*)(strTest->varadress)) =0;
            }
        }
        else if(strTest->vartype=="variant")
            *((variant*)(strTest->varadress)) = readstring;
        else if(strTest->vartype=="write")
        {
            SendMessage(consoleedit, EM_REPLACESEL, 0, (LPARAM)(char*)strTest->varvalue);
        }
        readstring="";
    }};


    bool blnShowCaret=true;
    COORD getlastcursorpos{0};

    WNDPROC wpOrigRichEditProc;

    static LRESULT APIENTRY RichEditProc(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
    {
        consolewindow *richedit = (consolewindow *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);

        switch(uMsg)
        {
            case WM_CREATE:
            {
                richedit->ShowCaret(true);
            }
            break;
            case WM_KEYDOWN:
            {
                if((wParam==VK_BACK) && (richedit->blnread==true))
                {
                    if(richedit->readstring.size()>=1)
                    {
                        richedit->readstring.pop_back();
                    }
                    else if (richedit->readstring.size()==0)
                        return 0;
                }
                else if(((wParam==VK_BACK) || (wParam==VK_RETURN)) && (richedit->blnread==false))
                    return 0;
            }
            break;

            case WM_RBUTTONUP:
            {
                richedit->ShowCaret(!richedit->ShowCaret());

            }
            break;

            case WM_KEYUP:
            {
                if((wParam==VK_RETURN) && (richedit->blnread==true))
                {
                    richedit->mtRead.SetCondition();
                    richedit->blnread=false;

                }
                else if((wParam==VK_RETURN) && (richedit->blnread==false))
                    return 0;
            }
            break;

            case WM_PAINT:
            {
                if(richedit->Paint.IsValid() ==true)
                {
                    richedit->ShowCaret(false);
                    PAINTSTRUCT ps;
                    BeginPaint(richedit->consoleedit,&ps);
                    string windowtext;
                    GetWindowString(richedit->consoleedit,windowtext);
                    RECT textrect;
                    DrawText(ps.hdc,windowtext.c_str(),-1,&textrect,DT_EXPANDTABS | DT_CALCRECT);
                    if(DrawText(ps.hdc,windowtext.c_str(),-1,&textrect,DT_EXPANDTABS)==0)
                        DebugText("error not text");
                    richedit->Paint(richedit->consoleedit,ps.hdc);
                    EndPaint(richedit->consoleedit,&ps);
                }
            }
            break;
            case WM_CHAR:
            {
                if(richedit->blnread==true && wParam!=VK_BACK)
                {
                    richedit->readstring=richedit->readstring + char(wParam);
                }
                else
                    return 0;

            }
            break;
        }
        return CallWindowProc(richedit->wpOrigRichEditProc,hwndDlg,uMsg,wParam,lParam);
    }

public:
    //event(Paint,(HWND hwndWindow, HDC hdcWindow))=NULL;
    Event<HWND, HDC>Paint;
    //event(Create)=NULL;
    Event <> Create;
    event(GetFocus)=nullptr;

    consolewindow(string strTitle="")
    {
        static int i=0;
        i=i+1;
        if(strTitle=="")
           strTitle="Console " + to_string(i);
        frmconsolewindow.Text=strTitle;

        LoadLibrary(TEXT("Msftedit.dll"));
        consoleedit = CreateWindowEx(WS_EX_NOACTIVATE,"RICHEDIT50W", TEXT(""),
                   WS_CHILD | WS_VISIBLE | ES_WANTRETURN | ES_MULTILINE | ES_AUTOHSCROLL|ES_AUTOVSCROLL | WS_VSCROLL | WS_HSCROLL, 0, 0,
                   frmconsolewindow.width-16, frmconsolewindow.height -38 , frmconsolewindow, 0, GetModuleHandle(0), 0 ) ;
        wpOrigRichEditProc=(WNDPROC)SetWindowLong(consoleedit,GWL_WNDPROC,(LONG_PTR)consolewindow::RichEditProc);
        SetWindowLongPtr(consoleedit,GWLP_USERDATA,(LONG) this);
        SendMessage( consoleedit, EM_SETBKGNDCOLOR, 0, RGB( 0,0,0 ) );
        CHARFORMAT2 cf;
        cf.dwEffects =0;
        cf.dwMask = CFM_COLOR;
        cf.crTextColor =RGB(255,255,255);
        cf.cbSize = sizeof(CHARFORMAT2);
        SendMessageA(consoleedit, EM_SETCHARFORMAT, SCF_DEFAULT, (LPARAM)&cf );
        //SetFocus(consoleedit);


        frmconsolewindow.Resize=[&]()
        {
            SetWindowPos(consoleedit,0,0,0,frmconsolewindow.width-16, frmconsolewindow.height -38 ,SWP_NOMOVE|SWP_NOREPOSITION|SWP_NOZORDER);
        };
        frmconsolewindow.Close=[&]()
        {
            blnread=false;
        };
        readdata.resize(100);
        //mtRead.SingleThreadAtTime(true);
        frmconsolewindow.GetFocus=[&]()
        {
            SetFocus(consoleedit);
            ShowCaret(true);
            if(GetFocus!=nullptr)
            {
                GetFocus();
            }
        };
        tmrCreate.Interval=100;
        tmrCreate.Start(false);
    }

    void SetCaretPos(COORD pos)
    {
        LONG characterIndexLine =0;
        characterIndexLine=(LONG) SendMessage(consoleedit, EM_LINEINDEX, (WPARAM) pos.Y-1,0);
        characterIndexLine+=(LONG)pos.X;
        CHARRANGE cr;
        cr.cpMin = characterIndexLine;
        cr.cpMax = characterIndexLine;
        SendMessage(consoleedit, EM_EXSETSEL, 0, (LPARAM)&cr);
    }

    COORD GetCaretPos( )
    {
        COORD pos;
        //getting the line where is the caret(WPARAM is -1 for the caret position):
        pos.Y=(LONG) SendMessage(consoleedit, EM_LINEFROMCHAR,(WPARAM)-1,0)+1;

        //getting the caret on text position:
        CHARRANGE cr;
        cr.cpMin = 0;
        cr.cpMax = 0;
        SendMessage(consoleedit, EM_EXGETSEL, 0, (LPARAM)&cr);

        //getting the 1st charater line position(is gived on text position). for use the caret position, we can use -1:
        LONG firstcharacter=SendMessage(consoleedit, EM_LINEINDEX, (WPARAM)-1,0);

        //the caret text position minus the 1st character on that line, will give me the x:
        pos.X=cr.cpMin-firstcharacter;
        return pos;
    }

    bool ShowCaret()
    {
        return blnShowCaret;
    }

    void ShowCaret(bool blShowCaret)
    {
        blnShowCaret=blShowCaret;
        if(blShowCaret==false)
        {
            getlastcursorpos=GetCaretPos();
            DestroyCaret();
        }
        else
        {
            CreateCaret(consoleedit,NULL,0,0);
            SetCaretPos(getlastcursorpos);
        }
    }

    void refresh()
    {
        InvalidateRect(consoleedit,NULL,FALSE);
    }

    void clear()
    {
        SetWindowText(consoleedit,"");
    }

    void SetBackGroundColor(COLORREF clrBackGround)
    {
        SendMessage(consoleedit, EM_SETBKGNDCOLOR, 0,clrBackGround);
    }

    void SetTextBackColor(COLORREF clrtxtColor, COLORREF clrBackColor=-1)
    {
        clrTextColor=clrtxtColor;
        if(clrBackColor!=-1)
            clrTextBackColor=clrBackColor;
    }

    operator HWND()
    {
        return frmconsolewindow;
    }

    void read()
    {
        //nothing
    }

    int i=-1;

    void read(variant &vrtValue)
    {
        //alterar o formato e a cor de texto
        CHARFORMAT2 cf ;
        cf.cbSize = sizeof( CHARFORMAT2 ) ;
        cf.dwMask = CFM_COLOR | CFM_BACKCOLOR | CFM_EFFECTS2 ;
        cf.crTextColor =clrTextColor;
        cf.dwEffects =0;
        cf.crBackColor = clrTextBackColor;
        SendMessage(consoleedit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
        i=i+1;
        readdata[i].varadress=&vrtValue;
        readdata[i].vartype="variant";
        mtRead(&readdata[i]);
        if(i==99)
            i=-1;
    }

    void read(string &txttext)
    {
        //alterar o formato e a cor de texto
        CHARFORMAT2 cf ;
        cf.cbSize = sizeof( CHARFORMAT2 ) ;
        cf.dwMask = CFM_COLOR | CFM_BACKCOLOR | CFM_EFFECTS2 ;
        cf.crTextColor =clrTextColor;
        cf.dwEffects =0;
        cf.crBackColor = clrTextBackColor;
        SendMessage(consoleedit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
        i=i+1;
        readdata[i].varadress=&txttext;
        readdata[i].vartype="string";
        mtRead(&readdata[i]);
        if(i==99)
            i=-1;
    }

    void read(char txttext[])//corrigir
    {
        CHARFORMAT2 cf ;
        cf.cbSize = sizeof( CHARFORMAT2 ) ;
        cf.dwMask = CFM_COLOR | CFM_BACKCOLOR | CFM_EFFECTS2 ;
        cf.crTextColor =clrTextColor;
        cf.dwEffects =0;
        cf.crBackColor = clrTextBackColor;
        SendMessage(consoleedit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
        i=i+1;
        if(readdata.size()<100)
            readdata.resize(i+1);
        readdata[i].varadress=&txttext;
        readdata[i].vartype="char*";
        mtRead(&readdata[i]);
        if(i==99)
            i=-1;
    }

    void read(double &txttext)//corrigir
    {
        CHARFORMAT2 cf ;
        cf.cbSize = sizeof( CHARFORMAT2 ) ;
        cf.dwMask = CFM_COLOR | CFM_BACKCOLOR | CFM_EFFECTS2 ;
        cf.crTextColor =clrTextColor;
        cf.dwEffects =0;
        cf.crBackColor = clrTextBackColor;
        SendMessage(consoleedit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
        i=i+1;
        readdata[i].varadress=&txttext;
        readdata[i].vartype="double";
        mtRead(&readdata[i]);
        if(i==99)
            i=-1;
    }

    void read(int &txttext)//corrigir
    {
        CHARFORMAT2 cf ;
        cf.cbSize = sizeof( CHARFORMAT2 ) ;
        cf.dwMask = CFM_COLOR | CFM_BACKCOLOR | CFM_EFFECTS2 ;
        cf.crTextColor =clrTextColor;
        cf.dwEffects =0;
        cf.crBackColor = clrTextBackColor;
        SendMessage(consoleedit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
        i=i+1;
        readdata[i].varadress=&txttext;
        readdata[i].vartype="int";
        mtRead(&readdata[i]);
        if(i==99)
            i=-1;
    }

    template<typename first_t, typename... rest_t>
    void read(first_t &first, rest_t... rest)
    {
        read(first);
        read(rest...); //function is recursive
                        //when the parameter pack becomes empty
                        //write(); will be called with no parameters
                        //which is why we declare it above
    }

    //write:

    void write(variant strText)
    {
        CHARFORMAT2 cf ;
        cf.cbSize = sizeof( CHARFORMAT2 ) ;
        cf.dwMask = CFM_COLOR | CFM_BACKCOLOR | CFM_EFFECTS2 ;
        cf.crTextColor =clrTextColor;
        cf.dwEffects =0;
        cf.crBackColor = clrTextBackColor;
        SendMessage(consoleedit, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf );
        i=i+1;
        readdata[i].vartype="write";
        readdata[i].varvalue=strText;
        mtRead(&readdata[i]);
        if(i==99)
            i=-1;
    }

    void write()
    {
        //nothing
    }

    template<typename first_t, typename... rest_t>
    void write(first_t first, rest_t... rest)
    {
        write((variant)first);
        write(rest...);
    }

    ~consolewindow()
    {
        mtRead.SetCondition();
        blnread=false;
    }

};

the read() give me problems when i try, on Debugger, exit the program.

can anyone advice me?

Share this post


Link to post
Share on other sites
Advertisement

Actually no. Nobody can advise you, because you're just wasting their time. Your earlier thread was a problem description of almost exactly the same level of detail, and you were prompted several times to provide clarifying details related to what your debugger tells you about the problem, and you didn't. Either because you don't know how to use your debugger or because you're willfully ignoring it expecting other people to do your work for you.

 

Learn to use your debugger, please. 

 

Once you've got a basic grasp of it, you may re-post your question provided you also describe in detail what the error is, where in your code it occurs, and what else your debugging has told you. Simply providing massive code dumps and asking "what is wrong with my code" over and over isn't really acceptable and takes advantage of the rest of the community.

Edited by Josh Petrie

Share this post


Link to post
Share on other sites

This topic is 817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!