gpt4 book ai didi

c++ - 如何处理线程竞速(C++;TinyThread++)?

转载 作者:行者123 更新时间:2023-11-28 03:28:19 34 4
gpt4 key购买 nike

这是主要代码的示例(“Library/stack.h”并不重要,但无论如何,它是 this previous question of mine 中包含的最后一个源代码):

#include <stdlib.h>
#include <time.h>

#include <iostream>
#include <tinythread.h>

#include "Library/stack.h"

using namespace std;
using namespace tthread;

#define BOULDERspd 100

// ========================================================================= //

struct Coord {
int x, y;
};

int randOneIn (float n) {
return ((int) (n * (rand() / (RAND_MAX + 1.0))));
}
int randOneIn (int n) {
return ((int) ((float) n * (rand() / (RAND_MAX + 1.0))));
}

// ========================================================================= //

#include <windows.h>
void gotoxy (int column, int line) {
if ((column >= 0) && (line >= 0)) {
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition(
GetStdHandle( STD_OUTPUT_HANDLE ),
coord
);
}
}

void gotoxy (Coord pos) {
gotoxy(pos.x, pos.y);
}

// ========================================================================= //

void render (char image, Coord pos) {
gotoxy(pos);
cout << image;
}

void unrender (Coord pos) {
gotoxy(pos);
cout << ' ';
}

// ========================================================================= //

char randimage (void) {
return (rand() % 132) + 123;
}

mutex xylock;

class Boulder {
char avatar;
Coord pos;

public:
Boulder (int inix) {
pos.x = inix;
pos.y = 0;

avatar = randimage();
};

void fall (void) {

unrender(pos);
pos.y++;
render(avatar, pos);

Sleep(BOULDERspd);
};

void live (void) {
do {
fall();
} while (y() < 20);
die();
};

void die (void) {
unrender(pos);
pos.y = 0;
};

int x (void) { return pos.x; };
int y (void) { return pos.y; };
};

// ========================================================================= //

class thrStack: public Stack<thread*> {
public:
thrStack (): Stack<thread*> () { };

void pushNrun (thread* elem) {
push(elem);
top->core->joinable();
}
};

void randBoulder (void* arg) {
srand(time(NULL));
Boulder boulder(rand() % 40);

boulder.live();
}

void Boulders (void* arg) {
srand(time(NULL));
thrStack stack;

do {
stack.pushNrun(new thread (randBoulder, 0));
Sleep(rand() % 300);
} while(1);
}

// ========================================================================= //
// ========================================================================= //

int main() {
thread raining (Boulders, 0);

raining.join();
}

我是多线程的新手,所以,为了摆弄它,我正在尝试制作一个程序,使随机字符不断从屏幕顶部掉落,就像下雨一样 ASCII 符号。

但是,我注意到我的编码中有一个小(大)错误:

bool xylock = false;

class Boulder {
char avatar;
Coord pos;

public:
Boulder (int inix) {
pos.x = inix;
pos.y = 0;

avatar = randimage();
};

void fall (void) {

unrender(pos);
pos.y++;
render(avatar, pos);

Sleep(BOULDERspd);
};

void live (void) {
do {
fall();
} while (y() < 20);
die();
};

void die (void) {
unrender(pos);
pos.y = 0;
};

int x (void) { return pos.x; };
int y (void) { return pos.y; };
};

因为 fall() 函数使用 gotoxy,它会更改“全局游标”,多次调用 gotoxy 会打乱程序的预期执行。如果你尝试按原样编译代码,你会得到不断切换位置的下降字母,并留下自己的垃圾。

是否有任何方法可以仅通过 TinyThread 为这种情况和 future 情况使用或实现锁?一般来说,C++ 中锁的实现逻辑是什么?


编辑:修改后的 fall();可以吗,驯鹿?

        void fall (void) {
lock_guard<mutex> guard(xylock);

unrender(pos);
pos.y++;
render(avatar, pos);

xylock.unlock();

Sleep(BOULDERspd);
};

最佳答案

您可以使用 tinythread 库:

http://tinythreadpp.bitsnbites.eu/doc/

具体看lock_guardmutex

multiple calls to gotoxy would mess up the intended execution of the program. If you try to compile the code as-is, you'd get falling letters that constantly switch position and leave garbage of themselves behind.

创建一个 mutex 对象进行同步,然后在您想要线程安全的函数中使用它创建一个本地 lock_guard。这个 mutex 可以在多个地方使用,也可以使用 lock_guard

关于c++ - 如何处理线程竞速(C++;TinyThread++)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13324918/

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