一个线程执行一个任务
需要某种方法来描述这个任务
Runnable类提供了一个公共接口来执行任意的任务显示火箭发射离地升空的倒计时
//: C11:LiftOff.h
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Demonstration of the Runnable interface.
#ifndef LIFTOFF_H
#define LIFTOFF_H
#include <iostream>
#include "Runnable.h"
class LiftOff : public ZThread::Runnable {
int countDown;
int id;
public:
LiftOff(int count, int ident = 0) :
countDown(count), id(ident) {}
~LiftOff() {
std::cout << id << " completed" << std::endl;
}
void run() {
while(countDown--)
std::cout << id << ":" << countDown << std::endl;
std::cout << "Liftoff!" << std::endl;
}
};
#endif // LIFTOFF_H ///:~
标识符id能看到任务的多个实例
如果只创建了单个实例
可以使用ident的默认值
析构函数允许读者看到一个任务已被正确地销毁
任务的run()函数不是单独的线程驱动
main()函数中仅被直接调用
//: C11:NoThread.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
#include "LiftOff.h"
int main() {
LiftOff launch(10);
launch.run();
getchar();
} ///:~
当一个类从Runnable派生出来的时候
必须有一个run()函数
但没有特别的
没有产生任何天生的线程处理能力
没有按照书上安装库 只是弄了两个文件 一个Config.h 一个Runnable.h 放到 Liftoff.h 目录下
输出
0:9
0:8
0:7
0:6
0:5
0:4
0:3
0:2
0:1
0:0
Liftoff!这个是Runnable.h
/*
* Copyright (c) 2005, Eric Crahen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ZTRUNNABLE_H__
#define __ZTRUNNABLE_H__
#include "Config.h"
namespace ZThread {
/**
* @class Runnable
*
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-16T17:45:35-0400>
* @version 2.2.11
*
* Encapsulates a Runnable task.
*/
class Runnable {
public:
/**
* Runnables should never throw in their destructors
*/
virtual ~Runnable() {}
/**
* Task to be performed in another thread of execution
*/
virtual void run() = 0;
};
}
#endif // __ZTRUNNABLE_H__
这个是Config.h
/*
* Copyright (c) 2005, Eric Crahen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ZTCONFIG_H__
#define __ZTCONFIG_H__
// =====================================================================================
// The following section describes the symbols the configure program will define.
// If you are not using configure (autoconf), then you make want to set these by
// uncommenting them here, or whatever other means you'd like.
// =====================================================================================
// (configure)
// Uncomment to disable actually changing the operating systems real thread priority
// #define ZTHREAD_DISABLE_PRIORITY 1
// (configure)
// Uncomment to disable compiling the interrupt() hook mechanisms.
// #define ZTHREAD_DISABLE_INTERRUPT 1
// (configure)
// Uncomment to select a Win32 ThreadOps implementation that uses _beginthreadex()
// otherwise, CreateThread() will be used for a Windows compilation
// #define HAVE_BEGINTHREADEX 1
// (configure)
// Uncomment to select a pthreads based implementation
// #define ZT_POSIX 1
// (configure)
// Uncomment to select a Windows based implementation that uses features not
// supported by windows 98 and 95
// #define ZT_WIN32 1
// (configure)
// Uncomment to select a Windows based implementation that does not use features not
// supported by windows 98 and 95, but may not be compatible with 64 bit or alpha systems
// #define ZT_WIN9X 1
// (configure)
// Uncomment to select a MacOS based implementation
// #define ZT_MACOS 1
// (configure)
// Uncomment to prefer vanilla implementations of primatives when possible
// #define ZT_VANILLA 1
// =====================================================================================
// The following section can be customized to select the implementation that is compiled
// Eventually, the configure program will be updated to define these symbols as well.
// =====================================================================================
// Uncomment to select very simple spinlock based implementations
// #define ZTHREAD_USE_SPIN_LOCKS 1
// Uncomment to select the vannila dual mutex implementation of FastRecursiveLock
// #define ZTHREAD_DUAL_LOCKS 1
// Uncomment to select a POSIX implementation of FastRecursiveLock that does not
// spin, but instead sleeps on a condition variable.
// #define ZTHREAD_CONDITION_LOCKS 1
// Uncomment if you want to eliminate inlined code used as a part of some template classes
// #define ZTHREAD_NOINLINE
// Uncomment if you want to compile a DLL version of the library. (Win32)
// #define ZTHREAD_EXPORTS 1
// Uncomment if you want to compile a client using the DLL version of the library. (Win32)
// #define ZTHREAD_IMPORTS 1
// ===================================================================================
// The following section will attempt to guess the best configuration for your system
// ===================================================================================
// Select an implementation by checking out the environment, first looking for
// compilers, then by looking for other definitions that could be present
#if !defined(ZT_POSIX) && !defined(ZT_WIN9X) && !defined(ZT_WIN32) && !defined(ZT_MACOS)
// Check for well known compilers
#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__BCPLUSPLUS__) || defined(__MINGW32__)
# define ZT_WIN32
#elif defined(__CYGWIN__)
# define ZT_POSIX
// Check for well known platforms
#elif defined(__linux__) || \
defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || \
defined(__hpux) || \
defined(__sgi) || \
defined(__sun)
# define ZT_POSIX
// Check for definitions from well known headers
#elif defined(_POSIX_SOURCE) || defined(_XOPEN_SOURCE)
# define ZT_POSIX
#elif defined(WIN32_LEAN_AND_MEAN)
# define ZT_WIN32
#elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
# define ZT_MACOS
#else
# error "Could not select implementation, define ZT_WIN9X, ZT_WIN32, ZT_POSIX or ZT_MACOS"
#endif
#endif
// Once an implementation has been selected, configure the API decorator
// for shared libraries if its needed.
#if defined(ZTHREAD_SHARED) // Compatibility w/ past releases
# define ZTHREAD_IMPORTS
#elif defined(ZTHREAD_STATIC)
# undef ZTHREAD_EXPORTS
# undef ZTHREAD_IMPORTS
#endif
// Windows users will get a static build by default, unless they
// define either ZTHREAD_IMPORTS or ZTHREAD_EXPORTS. Client code
// of a dll version of this library should define the first flag;
// To build the dll version of this library, define the second.
#if defined(ZTHREAD_IMPORTS) && defined(ZTHREAD_EXPORTS)
# error "Import and export declarations are not valid"
#else
# if defined(ZTHREAD_IMPORTS)
# define ZTHREAD_API __declspec(dllimport)
# elif defined(ZTHREAD_EXPORTS)
# define ZTHREAD_API __declspec(dllexport)
# else
# define ZTHREAD_API
# endif
#endif
// Once the API decorator is configured, create a macro for
// explicit template instantiation (whose need can hopefully
// be removed from the library)
#if defined(ZTHREAD_EXPORTS)
# define EXPLICIT_TEMPLATE(X) template class __declspec( dllexport ) X;
#elif defined(ZTHREAD_IMPORTS)
# define EXPLICIT_TEMPLATE(X) template class __declspec( dllimport ) X;
#else
# define EXPLICIT_TEMPLATE(X)
#endif
// Give libc a hint, should be defined by the user - but people tend
// to forget.
#if !defined(REENTRANT)
# define REENTRANT
#endif
#if !defined(_REENTRANT)
# define _REENTRANT
#endif
#if defined(_MSC_VER)
# pragma warning(disable:4275)
# pragma warning(disable:4290)
# pragma warning(disable:4786)
# pragma warning(disable:4251)
# pragma warning(disable:4355)
#endif
// Ensure that only one implementation is selected
#if \
(defined(ZT_POSIX) && defined(ZT_WIN32)) \
|| (defined(ZT_POSIX) && defined(ZT_WIN9X)) \
|| (defined(ZT_WIN32) && defined(ZT_WIN9X))
# error "Only one implementation should be selected!"
#endif
#if defined(ZTHREAD_NOINLINE)
# define ZTHREAD_INLINE
#else
# define ZTHREAD_INLINE inline
#endif
#endif // __ZTCONFIG_H__