功能: 将整个进程都休眠的
功能: 将某个线程休眠
功能: 线程调用该方法时,主动让出CPU,并且不参与CPU的本次调度,从而让其他线程有机会运行。在后续的调度周期里再参与CPU调度。这是主动放弃CPU的方法接口。
功能: 线程调用该方法时,同样会让出CPU,并且休眠一段时间,从而让其他线程有机会运行。等到休眠结束时,才参与CPU调度。这也是主动放弃CPU的方法。
this_thread::yield()方法让出CPU的时间是不确定的,并且以CPU调度时间片为单位,yield()的实现依赖于操作系统CPU调度策略,在不同的操作系统或者同一个操作系统的不同调度策略下,表现也可能是不同的。
而sleep_for()让出CPU的时间是固定的。
#include
#include
#include
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任务线程.
thread t1(task_01, i);
t1.detach();//线程与进程分离,各自执行,顺序是混换的.
//t1.join();//主进程等待线程执行结束,然后主进程开始执行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
//this_thread::sleep_for(chrono::nanoseconds(1));
//this_thread::yield;
//sleep(1);
return 0;
}
打印
xxx------->main(), line = 20
#include
#include
#include
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任务线程.
thread t1(task_01, i);
t1.detach();//线程与进程分离,各自执行,顺序是混换的.
//t1.join();//主进程等待线程执行结束,然后主进程开始执行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
this_thread::sleep_for(chrono::nanoseconds(1));
//this_thread::yield;
//sleep(1);
return 0;
}
打印
xxx------->main(), line = 20
#include
#include
#include
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任务线程.
thread t1(task_01, i);
t1.detach();//线程与进程分离,各自执行,顺序是混换的.
//t1.join();//主进程等待线程执行结束,然后主进程开始执行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
//this_thread::sleep_for(chrono::nanoseconds(1));
this_thread::yield();
//sleep(1);
return 0;
}
打印
xxx------->main(), line = 20
#include
#include
#include
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任务线程.
thread t1(task_01, i);
t1.detach();//线程与进程分离,各自执行,顺序是混换的.
//t1.join();//主进程等待线程执行结束,然后主进程开始执行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
//this_thread::sleep_for(chrono::nanoseconds(1));
//this_thread::yield();
//sleep(1);
usleep(100);//100us
return 0;
}
xxx------->main(), line = 20
xxx------->task_01(), line = 7
xxx------->task_01(), line = 10, i = 1000
#include
#include
#include
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(i < 1000)
i++;
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
int main(){
int i = 0, j = 0;
//1.任务线程.
thread t1(task_01, i);
t1.detach();//线程与进程分离,各自执行,顺序是混换的.
//t1.join();//主进程等待线程执行结束,然后主进程开始执行.
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
//this_thread::sleep_for(chrono::nanoseconds(1));
//this_thread::yield();
sleep(1);
//usleep(100);
return 0;
}
xxx------->main(), line = 20
xxx------->task_01(), line = 7
xxx------->task_01(), line = 10, i = 1000
1.this_thread::sleep_for(chrono::nanoseconds(1))和this_thread::yield()是cpu自行调度,带有不确定性,跟系统有关.
2.sleep(1)和usleep(100)确实延时了,等待线程执行.
#include
#include
#include
using namespace std;
void task_01(int i){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
if(i < 1000){
i++;
this_thread::sleep_for(chrono::nanoseconds(1));
}
printf("xxx------->%s(), line = %d, i = %d\n",__FUNCTION__,__LINE__,i);
}
void main_thread(int j){
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
while(j < 1000){
//this_thread::yield();
j++;
}
printf("xxx------->%s(), line = %d, j = %d\n",__FUNCTION__,__LINE__,j);
}
int main(){
int i = 0, j = 0;
thread t1(task_01, i);
main_thread(j);
t1.detach();
//t1.join();
printf("xxx------->%s(), line = %d\n",__FUNCTION__,__LINE__);
return 0;
}