Share via


Abort() has been called

Question

Monday, February 11, 2013 10:48 PM

Hello im trying to use threads, but i cant stop the threads without getting the error: Abort() has been called.

using namespace std;

void print1()
{
    //while(true)
    cout << "Function 1" << endl;
}

void print2()
{
    //while(true)
    cout << "Function 2" << endl;
}

int main(int argc, char argv[])
{
    //Creates Threads
    thread PrimaryThread(print1);
    thread SecondaryThread(print2);

    //Assigns the threas to pointers
    thread* pPrimeThread = &PrimaryThread;
    thread* pSecondaryThread = &PrimaryThread;

    LPDWORD pExitCode = 0;
    if(GetExitCodeThread(pPrimeThread,pExitCode) != 0)
    {
        ExitThread((DWORD)pExitCode);
        cout << "Thread Closed" << endl;

        if(CloseHandle(pPrimeThread))
        {
            cout << "Handle closed" << endl;
        }

    }

    system("pause");
    return 0;

}

All replies (3)

Tuesday, February 12, 2013 1:50 PM âś…Answered

On 2/12/2013 3:36 AM, Farmek wrote:

But it still gives me a "Abort() has been called" when i try to exit the application.

You must call join() or detach() methods on thread object before that object is destroyed. If ~thread destructor is reached while the object is still attached to a thread of execution (in other words, while joinable() returns true), the destructor calls terminate().

Igor Tandetnik


Monday, February 11, 2013 11:05 PM

On 2/11/2013 5:48 PM, Farmek wrote:

Hello im trying to use threads, but i cant stop the threads without getting the error: Abort() has been called.

using namespace std;

void print1()
{
        //while(true)
        cout << "Function 1" << endl;
}

void print2()
{
        //while(true)
        cout << "Function 2" << endl;
}

int main(int argc, char argv[])
{
        //Creates Threads
        thread PrimaryThread(print1);
        thread SecondaryThread(print2);

        //Assigns the threas to pointers
        thread* pPrimeThread = &PrimaryThread;
        thread* pSecondaryThread = &PrimaryThread;

        LPDWORD pExitCode = 0;
        if(GetExitCodeThread(pPrimeThread,pExitCode) != 0)
        {
                ExitThread((DWORD)pExitCode);
                cout << "Thread Closed" << endl;

                if(CloseHandle(pPrimeThread))
                 {
                         cout << "Handle closed" << endl;
                 }

        }

        system("pause");
         return 0;

}

Your code makes no sense whatsoever. It's just a mish-mash of API calls randomly strung together. What exactly are you trying to achieve?

If you want to wait for std::thread to finish executing, you call its join() method. join() doesn't return until the thread completes.

You can use native_handle() method to get a HANDLE you can pass to Windows API functions (this, of course, renders the code non-portable, at which point it's not quite clear why you would want to use std::thread).

Igor Tandetnik


Tuesday, February 12, 2013 8:36 AM

So i removed the code that i didn't really need.

using namespace std;

void print1()
{
    cout << "Function 1" << endl;
}

void print2()
{
    cout << "Function 2" << endl;
}

int main(int argc, char argv[])
{
    //Creates Threads
    thread PrimaryThread(print1);
    thread SecondaryThread(print2);

    system("pause");
    return 0;

}

But it still gives me a "Abort() has been called" when i try to exit the application.