0xC0000005 多线程动画错误

0xC0000005 Error on Multi Thread Animation

所以我使用 C++ 控制台制作了一个应用程序,如下所示,然后我收到错误 0x0000005。

第一次 运行 它像往常一样工作。谁能帮我解决这个问题?

我正在将 Code::Blocks IDE 与 Borland C++ 5.5 一起使用,我打算将其纳入 Borland C++ 5.02

#include <windows.h>
#include <stdio.h>
#include <dos.h>
#include <iostream.h>
#include <conio.h>

void linesmov(int mseconds, int y);

void linesmov(int mseconds, int y)
{
    int i=0;
    while (true)
    {
        i=i+1;
        // Or system("cls"); If you may...
        gotoxy(i,y);   
        cout << "____||____||____"; 
        gotoxy(i-1,y);
        cout << " ";
        Sleep(mseconds);
        if (i>115)
        {     
            i=0;  
            for(int o = 0; o < 100; o++)
            {
                gotoxy(0,y);   
                cout << "                  ";
            }
        }
    }
}

DWORD WINAPI mythread1(LPVOID lpParameter)
{
    printf("Thread inside %d \n", GetCurrentThreadId());
    linesmov(5,10);
    return 0;
}
DWORD WINAPI mythread2(LPVOID lpParameter)
{
    printf("Thread inside %d \n", GetCurrentThreadId());
    linesmov(30,15);
    return 0;
}

int main(int argc, char* argv[])
{
    HANDLE myhandle1;
    DWORD mythreadid1;
    HANDLE myhandle2;
    DWORD mythreadid2;
    myhandle1 = CreateThread(0,0,mythread1,0,0,&mythreadid1);
    myhandle2 = CreateThread(0,0,mythread2,0,0,&mythreadid2);
    printf("Thread after %d \n", mythreadid1);

    getchar();
    return 0;
}

a) 不通过 std::cout 输出的两个 gotoxy 都不是线程安全的/同步的。您需要 process-wide 个互斥锁来同步

b) 异常可能是因为您没有在 main 中使用 WaitForMultipleObjects 来等待线程完成。根据硬件和优化,main 可能会在线程完成工作之前退出。

包括我在内的评论中的所有这些解决方案绝对不是应该如何完成的方式。主要问题是线程之间缺乏同步并且缺乏处理它们的终止。此外,应检查每个函数的 thread-safe 兼容性或包装以匹配它。

考虑到 std::cout 自 c++11 以来我们有一些数据竞争保证:

Concurrent access to a synchronized (§27.5.3.4) standard iostream object’s formatted and unformatted input (§27.7.2.1) and output (§27.7.3.1) functions or a standard C stream by multiple threads shall not result in a data race (§1.10). [ Note: Users must still synchronize concurrent use of these objects and streams by multiple threads if they wish to avoid interleaved characters. — end note ]

所以根据这个注释,同步原语的 lask 被忽略了。

正在考虑处理线程终止。

HANDLE threadH = CreateThread(...);
...
TerminateThread(threadH, 0); // Terminates a thread.
WaitForSingleObject(threadH, INFINITE); // Waits until the specified object is in the signaled state or the time-out interval elapses.
CloseHandle(threadH); // Closes an open object handle.

TerminateThread(), but be aware of this solution, because ..

WaitForSingleObject()

这只是 thread-safe 道路的第一步。

我想推荐 Anthony Williams 的《C++ 并发实战:实用多线程》以供进一步阅读。

同步输出的粗鲁解决方案

#include <Windows.h>
#include <iostream>
#include <mutex>

std::mutex _mtx; // global mutex

bool online = true; // or condition_variable

void gotoxy(int x, int y)
{
    COORD c = { x, y };
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}

void linesmov(int mseconds, int y) {
    int i = 0;
    while (online) {
        i = i + 1;
        // Or system("cls"); If you may...

        _mtx.lock(); // <- sync here
        gotoxy(i, y);
        std::cout << "____||____||____"; gotoxy(i - 1, y);
        std::cout << " ";
        _mtx.unlock();  

        Sleep(mseconds);
        if (i > 75)
        {
            i = 0;
            for (int o = 0; o < 60; o++)
            {
                _mtx.lock(); // <- sync here
                gotoxy(0, y);
                std::cout << "                  ";
                _mtx.unlock();
            }
        }
    }
}

DWORD WINAPI mythread1(LPVOID lpParameter)
{
    std::cout << "Thread 1" << GetCurrentThreadId() << std::endl;
    linesmov(5, 10);
    return 0;
}
DWORD WINAPI mythread2(LPVOID lpParameter)
{
    std::cout << "Thread 2" << GetCurrentThreadId() << std::endl;
    linesmov(30, 15);
    return 0;
}

int main(int argc, char* argv[])
{
    DWORD mythreadid1;
    DWORD mythreadid2;
    HANDLE myhandle1 = CreateThread(0, 0, mythread1, 0, 0, &mythreadid1);
    HANDLE myhandle2 = CreateThread(0, 0, mythread2, 0, 0, &mythreadid2);

    std::cout << "Base thread: " << GetCurrentThreadId() << std::endl;

    getchar();

    online = false;

    WaitForSingleObject(myhandle1, INFINITE);
    WaitForSingleObject(myhandle2, INFINITE);

    CloseHandle(myhandle1);
    CloseHandle(myhandle2);

    return 0;
}