Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / Win32

Stopwatch

Rate me:
Please Sign up or sign in to vote.
4.97/5 (29 votes)
3 Jan 2015CPOL6 min read 65.9K   1.5K   43  
Benchmark C++ std::vector vs raw arrays, move assignable/constructable & copy assignable/constructable
// hwinthread.h
#pragma once
#ifndef __HWINTHREAD_H__
#define __HWINTHREAD_H__

#include "hwindef.h"
#include "hwinlog.h"
#include "hwinsynch.h"

namespace harlinn
{
    namespace windows
    {
        namespace threading
        {
            enum class ThreadPriority
            {
                Idle = THREAD_PRIORITY_IDLE,
                Lowest = THREAD_PRIORITY_LOWEST,
                BelowNormal = THREAD_PRIORITY_BELOW_NORMAL,
                Normal = THREAD_PRIORITY_NORMAL,
                AboveNormal = THREAD_PRIORITY_ABOVE_NORMAL,
                Highest = THREAD_PRIORITY_HIGHEST,
                TimeCritical = THREAD_PRIORITY_TIME_CRITICAL,
                BackgroundBegin = THREAD_MODE_BACKGROUND_BEGIN,
                BackgroundEnd = THREAD_MODE_BACKGROUND_END
            };

            class CurrentThreadHandle;
            class ThreadHandle : public WaitableHandle
            {
                friend class CurrentThreadHandle;
                DWORD threadId;
                HWIN_EXPORT static HANDLE CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes,SIZE_T dwStackSize,
                                ThreadHandle* pThreadHandle,DWORD dwCreationFlags);

                HWIN_EXPORT static unsigned __stdcall ThreadStart(void* lpThreadParameter);
            public:
                typedef WaitableHandle Base;
                typedef ThreadPriority ThreadPriority_t;
            protected:
                HWIN_EXPORT ThreadHandle(HANDLE theThreadHandle, bool ownsHandle = true);
            public:
                HWIN_EXPORT ThreadHandle(DWORD stackSize = 0, bool stackSizeIsReservation = false);

                HWIN_EXPORT static std::shared_ptr<CurrentThreadHandle> Current();

                HWIN_EXPORT ThreadHandle& Start();
                HWIN_EXPORT DWORD ThreadId() const;

                HWIN_EXPORT ThreadPriority_t ThreadPriority() const;
                HWIN_EXPORT ThreadHandle& SetThreadPriority(ThreadPriority_t theThreadPriority);

                HWIN_EXPORT DWORD Suspend();
                HWIN_EXPORT DWORD Resume();

                HWIN_EXPORT DWORD ExitCode() const;

            protected:
                HWIN_EXPORT virtual DWORD DoOnRun();

            };


            class CurrentThreadData
            {
                friend class ThreadHandle;
                friend class CurrentThreadHandle;
                std::weak_ptr<ThreadHandle> threadHandle;
                DWORD threadId;
            public:
                HWIN_EXPORT CurrentThreadData();
                HWIN_EXPORT ~CurrentThreadData();

                std::shared_ptr<ThreadHandle> ThreadHandle() const
                {
                    return threadHandle.lock();
                }
            };

            class CurrentThreadHandle : public SystemHandle
            {
                friend class ThreadHandle;
                __declspec(thread) static CurrentThreadData* currentThreadData;
            public:
                HWIN_EXPORT static void InitializeThreadData();
                HWIN_EXPORT static void FinalizeThreadData();
            public:
                typedef SystemHandle Base;
                typedef ThreadPriority ThreadPriority_t;

                HWIN_EXPORT CurrentThreadHandle();

                HWIN_EXPORT ThreadPriority_t ThreadPriority() const;
                HWIN_EXPORT CurrentThreadHandle& SetThreadPriority(ThreadPriority_t theThreadPriority);
                HWIN_EXPORT DWORD Suspend();

                HWIN_EXPORT CurrentThreadHandle& Sleep(DWORD milliSeconds);

                HWIN_EXPORT CurrentThreadHandle& Sleep(TimeSpan duration)
                {
                    DWORD millis = DWORD(duration.TotalMilliseconds());
                    return Sleep(millis);
                }

                HWIN_EXPORT bool Sleep(DWORD milliSeconds,bool alertable);

                bool Sleep(TimeSpan duration,bool alertable)
                {
                    DWORD millis = DWORD(duration.TotalMilliseconds());
                    return Sleep(millis, alertable);
                }
                


            };

        };
    };
};

#endif //__HWINTHREAD_H__

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Sea Surveillance AS
Norway Norway
Chief Architect - Sea Surveillance AS.

Specializing in integrated operations and high performance computing solutions.

I’ve been fooling around with computers since the early eighties, I’ve even done work on CP/M and MP/M.

Wrote my first “real” program on a BBC micro model B based on a series in a magazine at that time. It was fun and I got hooked on this thing called programming ...

A few Highlights:

  • High performance application server development
  • Model Driven Architecture and Code generators
  • Real-Time Distributed Solutions
  • C, C++, C#, Java, TSQL, PL/SQL, Delphi, ActionScript, Perl, Rexx
  • Microsoft SQL Server, Oracle RDBMS, IBM DB2, PostGreSQL
  • AMQP, Apache qpid, RabbitMQ, Microsoft Message Queuing, IBM WebSphereMQ, Oracle TuxidoMQ
  • Oracle WebLogic, IBM WebSphere
  • Corba, COM, DCE, WCF
  • AspenTech InfoPlus.21(IP21), OsiSoft PI


More information about what I do for a living can be found at: harlinn.com or LinkedIn

You can contact me at espen@harlinn.no

Comments and Discussions