zthread学习 实例四 让步、休眠、优先级
2015-05-19 21:21
387 查看
1、让步
Thread::yield()可以介入CPU的调度,使CPU强制放弃执行当前线程。
2、休眠
Thread:sleep()可以使线程停止执行一段时间。
Thread:sleep()可发抛出一个Interrupted_Exception,该异常必须在run()函数中捕获,因为异常是不会跨线程传播的,只能在线程内部处理。
3、优先级
Thread::setPriority()、Thread::getPriority(),可以人为的改变到线程执行的优先级,确保紧急的任务先执行。
实例如下:
[cpp] view
plaincopy
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "zthread/Runnable.h"
#include "zthread/Thread.h"
#include "zthread/PoolExecutor.h"
using namespace ZThread;
using namespace std;
const double pi = 3.141592653589793;
const double e = 2.718281828459;
class SimplePriorities : public Runnable
{
public:
SimplePriorities(int idn = 0): id(idn), nCountDown(5){}
~SimplePriorities(){}
friend ostream& operator << (ostream& os, const SimplePriorities& sp)
{
return os << "#" << sp.id << " priority : " <<Thread().getPriority() << "count : " << sp.nCountDown <<endl;
}
void run()
{
while (true)
{
for (int i = 0; i < 10000; i++)
{
d = d + (pi + e) /double(i);
}
cout << *this <<endl;
if (--nCountDown == 0) return;
}
}
private:
int nCountDown;
int id;
volatile double d;
};
int _tmain(int argc, _TCHAR* argv[])
{
try
{
Thread high(new SimplePriorities);
high.setPriority(Priority::High);
for (int i = 1; i < 5; i++)
{
Thread low(new SimplePriorities(i));
low.setPriority(Priority::Low);
}
cin.get();
}
catch (Synchronization_Exception& e)
{
cerr << e.what() <<endl;
}
cin.get();
return 0;
}
Thread::yield()可以介入CPU的调度,使CPU强制放弃执行当前线程。
2、休眠
Thread:sleep()可以使线程停止执行一段时间。
Thread:sleep()可发抛出一个Interrupted_Exception,该异常必须在run()函数中捕获,因为异常是不会跨线程传播的,只能在线程内部处理。
3、优先级
Thread::setPriority()、Thread::getPriority(),可以人为的改变到线程执行的优先级,确保紧急的任务先执行。
实例如下:
[cpp] view
plaincopy
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "zthread/Runnable.h"
#include "zthread/Thread.h"
#include "zthread/PoolExecutor.h"
using namespace ZThread;
using namespace std;
const double pi = 3.141592653589793;
const double e = 2.718281828459;
class SimplePriorities : public Runnable
{
public:
SimplePriorities(int idn = 0): id(idn), nCountDown(5){}
~SimplePriorities(){}
friend ostream& operator << (ostream& os, const SimplePriorities& sp)
{
return os << "#" << sp.id << " priority : " <<Thread().getPriority() << "count : " << sp.nCountDown <<endl;
}
void run()
{
while (true)
{
for (int i = 0; i < 10000; i++)
{
d = d + (pi + e) /double(i);
}
cout << *this <<endl;
if (--nCountDown == 0) return;
}
}
private:
int nCountDown;
int id;
volatile double d;
};
int _tmain(int argc, _TCHAR* argv[])
{
try
{
Thread high(new SimplePriorities);
high.setPriority(Priority::High);
for (int i = 1; i < 5; i++)
{
Thread low(new SimplePriorities(i));
low.setPriority(Priority::Low);
}
cin.get();
}
catch (Synchronization_Exception& e)
{
cerr << e.what() <<endl;
}
cin.get();
return 0;
}
相关文章推荐
- zthread学习 实例四 让步、休眠、优先级
- java线程--优先级、休眠、让步、插队实例
- zthread学习 实例五 非任务对象的生存期
- zthread学习 实例九 任务终止(二)——中断方式
- zthread学习 实例六 访问控制
- zthread学习 实例七 线程本地存储
- zthread学习 实例十三 线程间的协助(四)——读者、写者
- zthread学习 实例八 任务终止(一)——观赏植物园
- zthread学习 实例十四 信号量机制
- zthread学习 实例八 任务终止(一)——观赏植物园
- zthread学习 实例九 任务终止(二)——中断方式
- zthread学习 实例十五ZThread库基本结构的规纳
- zthread学习 实例十 线程间的协助(二)
- zthread学习 实例十 线程间的协助(一)
- zthread学习 实例二
- zthread学习 实例七 线程本地存储
- zthread学习 实例十一 信号量机制
- zthread学习 实例五 非任务对象的生存期
- zthread学习 实例十二 线程间的协助(三)——死锁
- zthread学习 实例十一 线程间的协助(二)