博文

对线程的简单封装(2006-10-10 14:23:00)

摘要:线程是操作系统的一个概念。通常操作系统通过一组逻辑上关联的API函数实现对线程的操作。但是在面向对象编程中,C方式的API调用显然与其他的对象代码不符合。因此提供我的封装方法,简单有效,以Windows平台为例。 #pragma once#include <windows.h> class Thread{public:    Thread(const char *name)    : hThread(0), dwThread(0), bTerminated(false), pname(name){}    ~Thread()    {        End();    }    void Start()    {        if(hThread == 0)            hThread = CreateThread(0,0,                            (LPTHREAD_START_ROUTINE)Thread::ThreadProc,                            this,0,&dwThread......

阅读全文(4149) | 评论:0

位指针的模拟(2006-08-02 20:08:00)

摘要:struct __bit_ptr{    typedef unsigned char byte;    typedef __bit_ptr bit_type;    byte * pbyte;    int pos;     __bit_ptr() : pbyte(0), pos(0) {}    __bit_ptr(const __bit_ptr& ptr) : pbyte(ptr.pbyte), pos(ptr.pos) {}    bool is_valid() {return pbyte!=0;}    __bit_ptr& operator++ ()    {        if(++pos>=8){ pos=0; pbyte++; }        return *this;    }    __bit_ptr operator++ (int)    {        __bit_ptr bp(*this);        if(++pos>=8){ pos=0; pbyte++; }        return bp;    }    __bit_ptr& operator += (int n)    {        pos += n;  ......

阅读全文(3849) | 评论:0