gpt4 book ai didi

c++ - 0xC0000005 多线程动画错误

转载 作者:行者123 更新时间:2023-11-28 04:45:12 25 4
gpt4 key购买 nike

所以我正在使用 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;
}

最佳答案

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

考虑 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() ,但请注意此解决方案,because ..

WaitForSingleObject()

这只是线程安全方式的第一步。

我想推荐 Anthony Williams 的 C++ Concurrency in Action: Practical Multithreading 进一步阅读。

同步输出的粗鲁解决方案

#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;
}

关于c++ - 0xC0000005 多线程动画错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49380133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com