Thread 类参考
#include <Thread.h>
Thread类继承关系图:
公有成员函数 | |
| Thread () | |
| Thread (const Task &, bool autoCancel=false) | |
| ~Thread () | |
| Destroy the Thread. | |
| bool | operator== (const Thread &t) const |
| Comparison operator. | |
| bool | operator!= (const Thread &t) const |
| Comparison operator. | |
| void | wait () |
| bool | wait (unsigned long timeout) |
| void | setPriority (Priority p) |
| Priority | getPriority () |
| bool | interrupt () |
| virtual bool | isCanceled () |
| virtual void | cancel () |
静态公有成员函数 | |
| bool | interrupted () |
| bool | canceled () |
| void | sleep (unsigned long timeout) |
| void | yield () |
细节描述
-
作者:
- Eric Crahen < http://www.code-foo.com>
-
日期:
- <2003-07-27T11:17:59-0400>
-
版本:
- 2.3.0
示例
开始一个任务
简单地通过构造一个Thread对象并且给它一个要执行的任务就可以开始一个线程. 线程会一直运行它的任务, 即使用于开始线程的 Thread 对象超出作用范围.
#include "zthread/Thread.h" #include <iostream> using namespace ZThread; class aRunnable : public Runnable { void run() { Thread::sleep(1000); std::cout << "Hello from another thread" << std::endl; } }; int main() { try { // Implictly constructs a Task Thread t(new aRunnable); } catch(Synchronization_Exception& e) { std::cerr << e.what() << std::endl; } std::cout << "Hello from the main thread" << std::endl; // Output: // Hello from the main thread // Hello from another thread return 0; }
等待一个任务
用户可以练习一下简单的同步,通过等待一个线程完成它的任务.
#include "zthread/Thread.h" #include <iostream> using namespace ZThread; class aRunnable : public Runnable { public: void run() { Thread::sleep(1000); std::cout << "Hello from another thread" << std::endl; } }; int main() { try { // Implictly constructs a Task Thread t(new aRunnable); t.wait(); } catch(Synchronization_Exception& e) { std::cerr << e.what() << std::endl; } std::cout << "Hello from the main thread" << std::endl; // Output: // Hello from another thread // Hello from the main thread return 0; }
共享一个任务
同一个任务可以被多个线程共享. 一个 Task 从 Runnable 构造, 并且这个 Task 对象按值拷贝并且传递到每个线程.
#include "zthread/Thread.h" #include <iostream> using namespace ZThread; class aRunnable : public Runnable { void run() { Thread::sleep(1000); std::cout << "Hello from another thread" << std::endl; } }; int main() { try { // Explictly constructs a Task Task task(new aRunnable); // Two threads created to run the same Task Thread t1(task); Thread t2(task); } catch(Synchronization_Exception& e) { std::cerr << e.what() << std::endl; } std::cout << "Hello from the main thread" << std::endl; // Output: // Hello from the main thread // Hello from another thread // Hello from another thread return 0; }
构造函数 & 析构函数 文档
|
| ||||||||||||
| 创建一个 Thread 对象,产生一个新的线程运行给定的任务.
|
成员函数文档
|
| 在一次操作中中断和取消这个线程. 如果将来检测线程的cancelation状态,线程会返回 true.
实现 Cancelable. |
|
| 检测当前线程是否被取消, 清除 interrupted 状态.
|
|
| 返回这个线程的优先级.
|
|
| 中断这个线程, 设置线程的 interrupted 状态. 这个状态通过下面三种方法中的一种清除. 如果这个方法被调用时,线程处于阻塞中, 线程会抛出一个Interrupted_Exception异常终止阻塞操作.
如果线程已经因为操作一个同步对象被阻塞,线程会抛出 Interrupted_Exception异常终止那个操作, 清除线程的 interrupted 状态,就像上面描述的第一种情况. 中断一个不在运行的线程不会有任何效果.
|
|
| 测试当前线程是否被 interrupt()ed, 清除 interruption 状态.
|
|
| 测试这个线程是否被取消. 如果在当前线程的上下文中调用, 中断状态会被清除.
实现 Cancelable. |
|
| 改变这个线程的优先级. 当操作系统支持时,会改变线程真正的优先级. 如果没有真正的优先级支持, 会模拟.
|
|
| 使当前执行的线程休眠指定的时间.
|
|
| 等待这个对象代表的线程完成任务. 调用线程阻塞直到这个对象代表的线程退出,或者超时.
实现 Waitable. |
|
| 等待这个对象代表的线程完成任务. 调用线程阻塞直到这个对象代表的线程退出.
实现 Waitable. |
|
| 使当前正在执行的线程放弃时间片, 允许调度程序分配执行时间到其他线程. |
这个类的文档从下面的文件生成:
- Thread.h
本文详细介绍了Thread类的功能和用法,包括其构造函数、析构函数、成员函数等,并提供了开始任务、等待任务和共享任务的示例代码。

204

被折叠的 条评论
为什么被折叠?



