如何使用 do while 函数但不停止其他代码
How to use do while function but also not stop other codes
我正在尝试解决此问题,如果您使用 do 和 while 代码,它会停止其他命令,但如果完成,则会继续这些命令。
for (int i = 0; i < 1000; i++) {
std::cout << "hey";
Sleep(1000);
}
for (int i = 0; i < 1000; i++) {
std::cout << "hey number 2";
Sleep(1000);
}
应该一起输出
hey
hey number 2
但它只是
hey
然后一旦完成就只是
hey number 2
我想你需要线程。
举个例子。它应该可以解决问题
#include <iostream>
#include <thread>
#include <synchapi.h>
using namespace std;
void fun1() {
for(int i = 0 ; i < 1000 ; i++) {
std::cout << "Hey";
Sleep(1000);
}
}
void fun2() {
for(int i = 0 ; i < 1000 ; i++) {
std::cout << "Hey you too";
Sleep(630);
}
}
int main()
{
std::thread first(fun1);
std::thread second(fun2);
first.join();
second.join();
std::cout << "done";
return 0;
}
我正在尝试解决此问题,如果您使用 do 和 while 代码,它会停止其他命令,但如果完成,则会继续这些命令。
for (int i = 0; i < 1000; i++) {
std::cout << "hey";
Sleep(1000);
}
for (int i = 0; i < 1000; i++) {
std::cout << "hey number 2";
Sleep(1000);
}
应该一起输出
hey
hey number 2
但它只是
hey
然后一旦完成就只是
hey number 2
我想你需要线程。
举个例子。它应该可以解决问题
#include <iostream>
#include <thread>
#include <synchapi.h>
using namespace std;
void fun1() {
for(int i = 0 ; i < 1000 ; i++) {
std::cout << "Hey";
Sleep(1000);
}
}
void fun2() {
for(int i = 0 ; i < 1000 ; i++) {
std::cout << "Hey you too";
Sleep(630);
}
}
int main()
{
std::thread first(fun1);
std::thread second(fun2);
first.join();
second.join();
std::cout << "done";
return 0;
}