Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

This is a thread.

Name: Anonymous 2007-04-25 6:13 ID:ipePewZ9

A thread, this is.

Name: Anonymous 2007-04-25 9:52 ID:ipePewZ9

MOAR LIEK


class Thread
{
public:
    template<typename F>
    Thread(F f)
    {
        ThreadFunc<F>* func = new ThreadFunc<F>(f);
       
        _hThread = CreateThread(NULL, 0, ThreadProxy<ThreadFunc<F> >, func, NULL, NULL);
    }
    template<typename F, typename P>
    Thread(F f, P p)
    {
        ThreadFunc<F, P>* func = new ThreadFunc<F, P>(f, p);

        _hThread = CreateThread(NULL, 0, ThreadProxy<ThreadFunc<F, P> >, func, NULL, NULL);
    }
    template<typename F, typename P1, typename P2>
    Thread(F f, P1 p1, P2 p2)
    {
        ThreadFunc<F, P1, P2> *func = new ThreadFunc<F, P1, P2>(f, p1, p2);

        _hThread = CreateThread(NULL, 0, ThreadProxy<ThreadFunc<F, P1, P2>>, func, NULL, NULL);
    }
    ~Thread()
    {
        CloseHandle(_hThread);
    }

    void Lock()
    {
        SuspendThread(_hThread);
    }

    void WaitForQuit(DWORD timeout)
    {
        WaitForSingleObject(_hThread, timeout);
    }

    void Unlock()
    {
        ResumeThread(_hThread);
    }

private:
    template<typename F, typename P1 = void, typename P2 = void>
    struct ThreadFunc
    {
        F func;
        P1 param1;
        P2 param2;

        void operator()()
        {
            func(param1, param2);
        }

        ThreadFunc(F f, P1 p1, P2 p2)
            : func(f), param1(p1), param2(p2)
        {}
    };
    template<typename F, typename P>
    struct ThreadFunc<F, P, void>
    {
        F func;
        P param;

        void operator()()
        {
            func(param);
        }

        ThreadFunc(F f, P p)
            : func(f), param(p)
        {}
    };

    template<typename F>
    struct ThreadFunc<F, void, void>
    {
        F func;

        void operator()()
        {
            func();
        }

        ThreadFunc(F f)
            : func(f)
        {}
    };

    template<typename ThreadFunc>
    static DWORD WINAPI ThreadProxy(LPVOID param)
    {
        ThreadFunc* pF = (ThreadFunc*)param;
        (*pF)();

        delete param;

        return 0;
    }

    HANDLE _hThread;
};

class Mutex
{
public:
    Mutex()
    {
        InitializeCriticalSection(&_cSection);
    }
    ~Mutex()
    {
        DeleteCriticalSection(&_cSection);
    }
private:
    void Aquire()
    {
        EnterCriticalSection(&_cSection);
    }
    void Release()
    {
        LeaveCriticalSection(&_cSection);
    }

    friend class Lock;
    CRITICAL_SECTION _cSection;
};

class Event
{
public:
    Event()
    {
        _hEvent = CreateEvent(0, TRUE, FALSE, "");
    }
    ~Event()
    {
        CloseHandle(_hEvent);
    }

    void Set()
    {
        SetEvent(_hEvent);
    }
    void Pulse()
    {
        PulseEvent(_hEvent);
    }
    void Reset()
    {
        ResetEvent(_hEvent);
    }
    bool Wait(unsigned int numMs)
    {
        return WaitForSingleObject(_hEvent, numMs) == WAIT_OBJECT_0;
    }
private:
    HANDLE _hEvent;
};

class Lock
{
public:
    Lock(Mutex &mutex) : _mutex(mutex)
    {
        mutex.Aquire();
    }
    ~Lock()
    {
        _mutex.Release();
    }
private:
    Mutex &_mutex;
};

#endif

KEKEKEKE

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List