C++ C++;不暂停控制台的循环延迟
我正在尝试创建一个函数,该函数将被反复多次执行,但在每个周期之间会有一个暂停 我尝试过使用“睡眠”,但这会暂停控制台。我在网上搜索过,只找到了在平时暂停控制台的答案 代码:C++ C++;不暂停控制台的循环延迟,c++,C++,我正在尝试创建一个函数,该函数将被反复多次执行,但在每个周期之间会有一个暂停 我尝试过使用“睡眠”,但这会暂停控制台。我在网上搜索过,只找到了在平时暂停控制台的答案 代码: int i; for(i=0; i<500; i++) { std::cout << "Hello" << std::endl; } inti; 对于(i=0;i,正如一些人评论的那样,您需要创建一个异步任务,以便在处理用户输入的同时完成一些工作 下面是一个关于如何使用线程完成此任务的
int i;
for(i=0; i<500; i++) {
std::cout << "Hello" << std::endl;
}
inti;
对于(i=0;i,正如一些人评论的那样,您需要创建一个异步任务,以便在处理用户输入的同时完成一些工作
下面是一个关于如何使用线程完成此任务的最简单的工作示例。它基于boost,因此您必须使用-lboost\u thread lboost\u system
链接它:
g++ test.cpp -lboost_thread -lboost_system -o test
为了解释您应该做什么,代码中有几个注释:
#include <queue>
#include <iostream>
#include <boost/thread.hpp>
// set by the main thread when the user enters 'quit'
bool stop = false;
boost::mutex stopMutex; // protect the flag!
// the function that runs in a new thread
void thread_function() {
// the following is based on the snippet you wrote
int i;
for(i=0; i<500; i++) {
// test if I have to stop on each loop
{
boost::mutex::scoped_lock lock(stopMutex);
if (stop) {
break;
}
}
// your task
std::cout << "Hello" << std::endl;
// sleep a little
::usleep(1000000);
}
}
int main() {
std::string str;
boost::thread t(thread_function);
while(true) {
std::cout << "Type 'quit' to exit: ";
// will read user input
std::getline(std::cin, str);
if (str == "quit") {
// the user wants to quit the program, set the flag
boost::mutex::scoped_lock lock(stopMutex);
stop = true;
break;
}
}
// wait for the async task to finish
t.join();
return 0;
}
#包括
#包括
#包括
//用户输入“退出”时由主线程设置
bool-stop=false;
boost::mutex stopMutex;//保护标志!
//在新线程中运行的函数
void thread_函数(){
//以下内容基于您编写的代码段
int i;
对于(i=0;i,正如一些人评论的那样,您需要创建一个异步任务,以便在处理用户输入的同时完成一些工作
下面是一个关于如何使用线程完成此任务的最简单的工作示例。它基于boost,因此您必须使用-lboost\u thread lboost\u system
链接它:
g++ test.cpp -lboost_thread -lboost_system -o test
为了解释您应该做什么,代码中有几个注释:
#include <queue>
#include <iostream>
#include <boost/thread.hpp>
// set by the main thread when the user enters 'quit'
bool stop = false;
boost::mutex stopMutex; // protect the flag!
// the function that runs in a new thread
void thread_function() {
// the following is based on the snippet you wrote
int i;
for(i=0; i<500; i++) {
// test if I have to stop on each loop
{
boost::mutex::scoped_lock lock(stopMutex);
if (stop) {
break;
}
}
// your task
std::cout << "Hello" << std::endl;
// sleep a little
::usleep(1000000);
}
}
int main() {
std::string str;
boost::thread t(thread_function);
while(true) {
std::cout << "Type 'quit' to exit: ";
// will read user input
std::getline(std::cin, str);
if (str == "quit") {
// the user wants to quit the program, set the flag
boost::mutex::scoped_lock lock(stopMutex);
stop = true;
break;
}
}
// wait for the async task to finish
t.join();
return 0;
}
#包括
#包括
#包括
//用户输入“退出”时由主线程设置
bool-stop=false;
boost::mutex stopMutex;//保护标志!
//在新线程中运行的函数
void thread_函数(){
//以下内容基于您编写的代码段
int i;
对于(i=0;ii如果你想让用户继续使用控制台,你需要一个额外的线程。即使这样,输出也会与用户的输入混合在一起。请看和。@FrançoisAndrieux这只是一个例子。我的目标是在做一些后台任务时允许用户输入。@JohanDoe问题是,tr会带来不寻常的后果使用多线程甚至是这样的基本代码。当然可以允许用户使用一些基本的多线程处理在打印语句之间输入文本,但结果会出现交叉文本:“ThiHello\ns是一个用户在打印StateMHello\n语句时,将文本输入conHello\n。Hello\n”
(显然,\n
代码意味着换行符)创建基于文本的交互式用户界面是一项非常重要的任务。因此,我建议使用@JohanDoe这样的框架,说“这只是一个示例”没有真正的帮助。不清楚你真正想要什么或你是否在问一个问题。你应该包括一个详细、准确的解释和/或示例,这样人们就可以写出有用的答案,而不必推断你真正想要什么。如果你想运行的后台任务不是“将文本打印到控制台”,那么就不要写示例这会将文本打印到控制台。如果您想允许用户继续使用控制台,您将需要一个额外的线程。即使这样,输出也会与用户的输入混合。请参阅和。@FrançoisAndrieux这只是一个示例。只需允许用户在执行某些后台任务时输入是我的目标。@JohanDoe问题是尝试对这样的基本代码进行多线程处理会产生不寻常的后果。当然,允许用户使用一些基本的多线程处理在打印语句之间输入文本是可能的,但您将得到交错文本:“ThiHello\ns是一个用户,在打印StateMHello\nnts时,它会进入conHello\n解决方案。Hello\n”
(显然,\n
代码是换行符)创建一个交互式基于文本的用户界面是一项非常重要的任务。因此,我建议使用@JohanDoe这样的框架,并说“这只是一个示例”"没有真正的帮助。不清楚你真正想要什么或你是否在问一个问题。你应该包括一个详细、准确的解释和/或示例,这样人们就可以写出有用的答案,而不必推断你真正想要什么。如果你想运行的后台任务不是“将文本打印到控制台”,那么就不要写示例这会将文本打印到控制台。这不应该是boost::atomic stop;
?编译器对正在创建的线程一无所知,因此可以自由假设stop
在thread\u function()时不会更改
正在运行。它可能会将初始值存储在寄存器中,并且永远不会从内存中更新它。这不应该是boost::atomic stop;
?编译器对正在创建的线程一无所知,因此可以自由地假设stop
在thread\u function()时不会更改
正在运行。它可能会将初始值存储在寄存器中,而不会从内存中更新它。