C++中的多线程 - 显示动画直到另一个线程完成
序言:这是我第一次尝试使用具有多线程的任何语言编写程序。我没有先前在STD ::线程的经验,也没有描述多线程的程序。请让我知道您是否需要更多信息来回答此问题,或者我是否可以重新介绍任何内容以使我的问题更清晰。
假设我有一个简单的动画函数Animate
,该功能(目前)无限期地运行;另一个函数任务
,代表某些任意执行的代码。
#include <string>
// a simple rotating stick animation: | -> / -> - -> \
void animate()
{
std::string syms = "|/-\\";
while (true)
{
for (unsigned int ii = 0; ii < syms.length(); ++ii)
{
std::cout << syms[ii];
std::cout << std::string(1, '\b');
}
}
}
// arbitrary function
void task()
{
// code to do something else goes here
}
int main (int argc, char* argv[])
{
// execute task and animate at the same time;
// return from animate once task has completed
return 0;
}
我如何使用std :: thread
(或其他一些标题/库)允许task
和Animate
互相通信,以便任务
在“背景”和Animate
运行直到任务
已经完成?
Preface: this is my first attempt at writing a program in any language with multi-threading. I have no prior experience with std::thread, or with describing multi-threaded programs at all. Please let me know if you need more information to answer this question, or if I can reword anything to make my question clearer.
Suppose I have a simple animation function animate
, which (for now) runs indefinitely; and another function task
, which represents some arbitrary code to execute.
#include <string>
// a simple rotating stick animation: | -> / -> - -> \
void animate()
{
std::string syms = "|/-\\";
while (true)
{
for (unsigned int ii = 0; ii < syms.length(); ++ii)
{
std::cout << syms[ii];
std::cout << std::string(1, '\b');
}
}
}
// arbitrary function
void task()
{
// code to do something else goes here
}
int main (int argc, char* argv[])
{
// execute task and animate at the same time;
// return from animate once task has completed
return 0;
}
How can I use std::thread
(or some other header/library) to allow task
and animate
to communicate with each other, such that task
runs in the "background" and animate
runs until task
has completed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经使用了您的示例代码,并试图用
std :: thread
从您的意图中填充空白。我添加了一些内联评论来解释正在发生的事情。如果不清楚的话,或者我弄错了,请随时在评论中询问。我想强调,虽然这个示例 使用
std :: thread
在玩具示例中创建并连接一个单独的线程。它不会以正确的方式在线程之间同步共享数据。
MT环境中的共享数据是事物变得毛茸茸的,最糟糕的是,它可以创建一些最难调试错误的地方。这就是为什么书籍和工作示例对于任何实质性多线程程序都很重要的原因。
如果您想进一步,我建议您推荐一些链接。
I've taken your example code and tried to fill in the blanks from your intention with
std::thread
. I've added some comments inline to explain what's going on. If something's not clear, or I got it wrong, feel free to ask in the comments.I want to stress though that this example only uses
std::thread
to create and join a separate thread in a toy example.It does not synchronize shared data between threads in a correct way.
Shared data in MT environments is where things get hairy and the worst thing is that it can create some of the hardest to debug bugs out there. That's why the books and working examples are important for any substantial multithreaded program.
If you want to take it a little further, here's some links I'd recommend.