我如何在我的程序中使用休眠时间?
How do I use sleep time in my program?
我写了一个关于多线程的示例程序来练习。我在使用 sleep_for(milliseconds) 函数的地方进退两难。
我正在尝试学习线程概念。所以把这当作我的实践,看看三个线程是如何同步的。为了了解它,我添加了睡眠。另外,我加sleep的目的是为了在控制台上更清楚地看到输出。
请帮助我了解如何决定我应该在哪个函数中使用 sleep_for。
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
using namespace std;
using std::chrono::duration;
using std::chrono::system_clock;
void oneLine(mutex & mut, int nr)
{
lock_guard<mutex> lg(mut);
cout << "Thread # " << nr << endl;
// Should I call sleep_for here?
std::this_thread::sleep_for(chrono::milliseconds(25));
}
void callFun(mutex & mut, int nr)
{
while(true)
{
oneLine(mut, nr);
this_thread::yield();
// or here?
std::this_thread::sleep_for(chrono::milliseconds(250));
}
}
int main()
{
mutex thMutex;
thread t1(callFun, ref(thMutex), 1);
thread t2(callFun, ref(thMutex), 2);
thread t3(callFun, ref(thMutex), 3);
t1.join();
t2.join();
t3.join();
}
在sleep_for或oneLine()中,我在哪里可以调用sleep_for callFun()。
谢谢
我认为您的示例非常适合学习多线程基础知识。这一切都取决于您要如何为案例 1 或案例 2 设计代码(请参阅下面代码中的标签)。
对于情况 1,您正在调用 oneLine 函数中的睡眠,以便在线程产生之前和互斥锁超出范围之前调用它,从而防止下一个线程完成 oneLine 函数,直到它可以获得互斥体上的锁。如果您关心在下一个线程获取互斥锁之前有一个暂停,则选择情况 1。这是一个设计决定,取决于您在代码中的应用程序。在情况 2 中,如果您想让另一个线程完成其 oneLine 调用而不必等待您为另一个线程指定的休眠期以放弃对互斥锁的锁定,则将其放在函数之后。在情况 2 中,您只关心同一线程之间有一个固定的暂停 运行。
编辑: 但是,在所有情况下,都无法保证这些线程将以任何预定义的顺序访问互斥量。我 运行 代码在情况 1 和情况 2 中有睡眠,没有睡眠,并且顺序不是 gua运行teed 是顺序的互斥锁。
只是 http://en.cppreference.com/w/cpp/thread/yield
关于 std::this_thread::yield 的注释
The exact behavior of this function depends on the implementation, in
particular on the mechanics of the OS scheduler in use and the state
of the system. For example, a first-in-first-out realtime scheduler
(SCHED_FIFO in Linux) would suspend the current thread and put it on
the back of the queue of the same-priority threads that are ready to
run (and if there are no other threads at the same priority, yield has
no effect).
因此您的代码的行为也将取决于 OS 调度程序。在我的例子中,当我 运行 它时,互斥锁从来没有确定的顺序。请参阅下面的屏幕截图,其中顺序不连续。
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
#include <stdlib.h> //needed added here
using std::chrono::duration;
using std::chrono::system_clock;
void oneLine(std::mutex & mut, int nr)
{
std::lock_guard<std::mutex> lg(mut);
std::cout << "Thread # " << nr << std::endl;
// Case1
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
}
void callFun(std::mutex & mut, int nr)
{
while(true)
{
oneLine(mut, nr);
std::this_thread::yield();
// Case2
//std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
int main(int argc, char **argv)
{
std::mutex thMutex;
std::thread t1(callFun, ref(thMutex), 1);
std::thread t2(callFun, ref(thMutex), 2);
std::thread t3(callFun, ref(thMutex), 3);
t1.join(); // will never get here, because thread t1 in infinite loop
t2.join(); // will never get here, because thread t2 in infinite loop
t3.join(); // will never get here, because thread t3 in infinite loop
return(0); // will never get here because waiting for threads t1, t2, t3 to finish
}
您的问题归结为:您是否应该在锁定互斥锁的情况下调用 sleep()
?还是解锁了互斥体?
这取决于你想模拟什么。你想模拟一些不经常发生的事情吗?然后 sleep()
在受保护的块之外。如果要模拟一个耗时较长的synchronized操作,那么sleep()
inside the protected block.
但是注意!
任何真正的程序保持互斥量锁定超过几微秒是设计审查的大红旗。长时间保持互斥锁锁定的程序不太可能执行良好。
永远不会,...
曾经,...
...sleep()
在任何 real 程序中的互斥体中。别这样好吗?
我写了一个关于多线程的示例程序来练习。我在使用 sleep_for(milliseconds) 函数的地方进退两难。 我正在尝试学习线程概念。所以把这当作我的实践,看看三个线程是如何同步的。为了了解它,我添加了睡眠。另外,我加sleep的目的是为了在控制台上更清楚地看到输出。
请帮助我了解如何决定我应该在哪个函数中使用 sleep_for。
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
using namespace std;
using std::chrono::duration;
using std::chrono::system_clock;
void oneLine(mutex & mut, int nr)
{
lock_guard<mutex> lg(mut);
cout << "Thread # " << nr << endl;
// Should I call sleep_for here?
std::this_thread::sleep_for(chrono::milliseconds(25));
}
void callFun(mutex & mut, int nr)
{
while(true)
{
oneLine(mut, nr);
this_thread::yield();
// or here?
std::this_thread::sleep_for(chrono::milliseconds(250));
}
}
int main()
{
mutex thMutex;
thread t1(callFun, ref(thMutex), 1);
thread t2(callFun, ref(thMutex), 2);
thread t3(callFun, ref(thMutex), 3);
t1.join();
t2.join();
t3.join();
}
在sleep_for或oneLine()中,我在哪里可以调用sleep_for callFun()。 谢谢
我认为您的示例非常适合学习多线程基础知识。这一切都取决于您要如何为案例 1 或案例 2 设计代码(请参阅下面代码中的标签)。
对于情况 1,您正在调用 oneLine 函数中的睡眠,以便在线程产生之前和互斥锁超出范围之前调用它,从而防止下一个线程完成 oneLine 函数,直到它可以获得互斥体上的锁。如果您关心在下一个线程获取互斥锁之前有一个暂停,则选择情况 1。这是一个设计决定,取决于您在代码中的应用程序。在情况 2 中,如果您想让另一个线程完成其 oneLine 调用而不必等待您为另一个线程指定的休眠期以放弃对互斥锁的锁定,则将其放在函数之后。在情况 2 中,您只关心同一线程之间有一个固定的暂停 运行。
编辑: 但是,在所有情况下,都无法保证这些线程将以任何预定义的顺序访问互斥量。我 运行 代码在情况 1 和情况 2 中有睡眠,没有睡眠,并且顺序不是 gua运行teed 是顺序的互斥锁。
只是 http://en.cppreference.com/w/cpp/thread/yield
关于 std::this_thread::yield 的注释The exact behavior of this function depends on the implementation, in particular on the mechanics of the OS scheduler in use and the state of the system. For example, a first-in-first-out realtime scheduler (SCHED_FIFO in Linux) would suspend the current thread and put it on the back of the queue of the same-priority threads that are ready to run (and if there are no other threads at the same priority, yield has no effect).
因此您的代码的行为也将取决于 OS 调度程序。在我的例子中,当我 运行 它时,互斥锁从来没有确定的顺序。请参阅下面的屏幕截图,其中顺序不连续。
#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
#include <stdlib.h> //needed added here
using std::chrono::duration;
using std::chrono::system_clock;
void oneLine(std::mutex & mut, int nr)
{
std::lock_guard<std::mutex> lg(mut);
std::cout << "Thread # " << nr << std::endl;
// Case1
std::this_thread::sleep_for(std::chrono::milliseconds(2500));
}
void callFun(std::mutex & mut, int nr)
{
while(true)
{
oneLine(mut, nr);
std::this_thread::yield();
// Case2
//std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
}
int main(int argc, char **argv)
{
std::mutex thMutex;
std::thread t1(callFun, ref(thMutex), 1);
std::thread t2(callFun, ref(thMutex), 2);
std::thread t3(callFun, ref(thMutex), 3);
t1.join(); // will never get here, because thread t1 in infinite loop
t2.join(); // will never get here, because thread t2 in infinite loop
t3.join(); // will never get here, because thread t3 in infinite loop
return(0); // will never get here because waiting for threads t1, t2, t3 to finish
}
您的问题归结为:您是否应该在锁定互斥锁的情况下调用 sleep()
?还是解锁了互斥体?
这取决于你想模拟什么。你想模拟一些不经常发生的事情吗?然后 sleep()
在受保护的块之外。如果要模拟一个耗时较长的synchronized操作,那么sleep()
inside the protected block.
但是注意!
任何真正的程序保持互斥量锁定超过几微秒是设计审查的大红旗。长时间保持互斥锁锁定的程序不太可能执行良好。
永远不会,...
曾经,...
...sleep()
在任何 real 程序中的互斥体中。别这样好吗?