X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/4cc4bfafe5a31cb96f35b3ec9b19fa2b0b3a4eef..3ed3a1c846d66410872a15f746c432311e306e42:/interface/thread.h diff --git a/interface/thread.h b/interface/thread.h index bfd661a200..5f07abb5ed 100644 --- a/interface/thread.h +++ b/interface/thread.h @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // Name: thread.h -// Purpose: documentation for wxCondition class +// Purpose: interface of wxCondition // Author: wxWidgets team // RCS-ID: $Id$ // Licence: wxWindows license @@ -33,8 +33,7 @@ @library{wxbase} @category{thread} - @seealso - wxThread, wxMutex + @see wxThread, wxMutex */ class wxCondition { @@ -57,7 +56,7 @@ public: Broadcasts to all waiting threads, waking all of them up. Note that this method may be called whether the mutex associated with this condition is locked or not. - + @see Signal() */ void Broadcast(); @@ -66,7 +65,7 @@ public: Returns @true if the object had been initialized successfully, @false if an error occurred. */ - bool IsOk(); + bool IsOk() const; /** Signals the object waking up at most one thread. If several threads are waiting @@ -75,7 +74,7 @@ public: signalled again to wake up any thread which may start waiting on it later. Note that this method may be called whether the mutex associated with this condition is locked or not. - + @see Broadcast() */ void Signal(); @@ -91,10 +90,10 @@ public: Wait without waking up any thread, the thread would still wait for another one and so it is important to ensure that the condition will be signalled after Wait or the thread may sleep forever. - + @returns Returns wxCOND_NO_ERROR on success, another value if an error occurred. - + @see WaitTimeout() */ wxCondError Wait(); @@ -104,7 +103,7 @@ public: This method is identical to Wait() except that it returns, with the return code of @c wxCOND_TIMEOUT as soon as the given timeout expires. - + @param milliseconds Timeout in milliseconds */ @@ -112,6 +111,7 @@ public: }; + /** @class wxCriticalSectionLocker @wxheader{thread.h} @@ -152,8 +152,7 @@ public: @library{wxbase} @category{thread} - @seealso - wxCriticalSection, wxMutexLocker + @see wxCriticalSection, wxMutexLocker */ class wxCriticalSectionLocker { @@ -171,6 +170,7 @@ public: }; + /** @class wxThreadHelper @wxheader{thread.h} @@ -204,8 +204,7 @@ public: @library{wxbase} @category{thread} - @seealso - wxThread + @see wxThread */ class wxThreadHelper { @@ -226,7 +225,7 @@ public: should call @ref wxThread::run GetThread()-Run to start running it. You may optionally specify the stack size to be allocated to it (Ignored on platforms that don't support setting it explicitly, eg. Unix). - + @returns One of: */ wxThreadError Create(unsigned int stackSize = 0); @@ -255,12 +254,13 @@ public: }; + /** @class wxCriticalSection @wxheader{thread.h} A critical section object is used for exactly the same purpose as - mutexes. The only difference is that under Windows platform + mutexes(). The only difference is that under Windows platform critical sections are only visible inside one process, while mutexes may be shared between processes, so using critical sections is slightly more efficient. The terminology is also slightly different: mutex may be locked (or @@ -276,8 +276,7 @@ public: @library{wxbase} @category{thread} - @seealso - wxThread, wxCondition, wxCriticalSectionLocker + @see wxThread, wxCondition, wxCriticalSectionLocker */ class wxCriticalSection { @@ -307,6 +306,7 @@ public: }; + /** @class wxThread @wxheader{thread.h} @@ -320,16 +320,15 @@ public: also makes it much easier to shoot oneself in the foot, so careful use of synchronization - objects such as mutexes or @ref overview_wxcriticalsection "critical sections" - is recommended. In addition, don't create global thread + objects such as mutexes() or @ref overview_wxcriticalsection "critical + sections" is recommended. In addition, don't create global thread objects because they allocate memory in their constructor, which will cause problems for the memory checking system. @library{wxbase} @category{thread} - @seealso - wxMutex, wxCondition, wxCriticalSection + @see wxMutex, wxCondition, wxCriticalSection */ class wxThread { @@ -340,13 +339,13 @@ public: does not create or start execution of the real thread -- for this you should use the Create() and Run() methods. The possible values for @a kind parameters are: - + @b wxTHREAD_DETACHED - + Creates a detached thread. - + @b wxTHREAD_JOINABLE - + Creates a joinable thread. */ wxThread(wxThreadKind kind = wxTHREAD_DETACHED); @@ -382,7 +381,7 @@ public: use a lot of threads (say several hundred), virtual adress space can get tight unless you explicitly specify a smaller amount of thread stack space for each thread. - + @returns One of: */ wxThreadError Create(unsigned int stackSize = 0); @@ -410,11 +409,11 @@ public: in fact calling a routine on any running wxThread should be avoided if possible. Instead, find a way to notify yourself when the thread has ended. Usually you only need to notify the main thread, in which case you can post - an event to it via wxPostEvent or + an event to it via wxPostEvent() or wxEvtHandler::AddPendingEvent. In the case of secondary threads you can call a routine of another class when the thread is about to complete processing and/or set the value - of a variable, possibly using mutexes and/or other + of a variable, possibly using mutexes() and/or other synchronization means if necessary. */ @@ -441,7 +440,7 @@ public: /** Returns the number of system CPUs or -1 if the value is unknown. - + @see SetConcurrency() */ static int GetCPUCount(); @@ -459,25 +458,25 @@ public: thread throughout the system during its existence (i.e. the thread identifiers may be reused). */ - unsigned long GetId(); + unsigned long GetId() const; /** Gets the priority of the thread, between zero and 100. The following priorities are defined: - + @b WXTHREAD_MIN_PRIORITY - + 0 - + @b WXTHREAD_DEFAULT_PRIORITY - + 50 - + @b WXTHREAD_MAX_PRIORITY - + 100 */ - int GetPriority(); + int GetPriority() const; /** Returns @true if the thread is alive (i.e. started and not terminating). @@ -486,14 +485,14 @@ public: no longer alive, it is not possible to call this function because the wxThread object no longer exists. */ - bool IsAlive(); + bool IsAlive() const; /** Returns @true if the thread is of the detached kind, @false if it is a joinable one. */ - bool IsDetached(); + bool IsDetached() const; /** Returns @true if the calling thread is the main application thread. @@ -503,14 +502,14 @@ public: /** Returns @true if the thread is paused. */ - bool IsPaused(); + bool IsPaused() const; /** Returns @true if the thread is running. This method may only be safely used for joinable threads, see the remark in IsAlive(). */ - bool IsRunning(); + bool IsRunning() const; /** Immediately terminates the target thread. @b This function is dangerous and @@ -577,25 +576,25 @@ public: after calling Create() but before calling Run(). The following priorities are already defined: - + @b WXTHREAD_MIN_PRIORITY - + 0 - + @b WXTHREAD_DEFAULT_PRIORITY - + 50 - + @b WXTHREAD_MAX_PRIORITY - + 100 */ void SetPriority(int priority); /** Pauses the thread execution for the given amount of time. - This function should be used instead of wxSleep by all worker - threads (i.e. all except the main one). + + This is the same as wxMilliSleep(). */ static void Sleep(unsigned long milliseconds); @@ -658,7 +657,7 @@ public: See @ref overview_deletionwxthread "wxThread deletion" for a broader explanation of this routine. */ - ExitCode Wait(); + ExitCode Wait() const; /** Give the rest of the thread time slice to the system allowing the other threads @@ -730,10 +729,10 @@ public: and could end your application prematurely. This is due to several reasons, including the underlying native API and the fact that wxThread does not run a GUI event loop similar to other APIs as MFC. - A workaround that works on some wxWidgets ports is calling wxMutexGUIEnter - before any GUI calls and then calling wxMutexGUILeave afterwords. However, + A workaround that works on some wxWidgets ports is calling wxMutexGUIEnter() + before any GUI calls and then calling wxMutexGUILeave() afterwords. However, the recommended way is to simply process the GUI calls in the main thread - through an event that is posted by either wxPostEvent or + through an event that is posted by either wxPostEvent() or wxEvtHandler::AddPendingEvent. This does not imply that calls to these classes are thread-safe, however, as most wxWidgets classes are not thread-safe, including wxString. @@ -741,6 +740,7 @@ public: }; + /** @class wxSemaphore @wxheader{thread.h} @@ -787,14 +787,14 @@ public: Increments the semaphore count and signals one of the waiting threads in an atomic way. Returns wxSEMA_OVERFLOW if the count would increase the counter past the maximum. - + @returns One of: */ wxSemaError Post(); /** Same as Wait(), but returns immediately. - + @returns One of: */ wxSemaError TryWait(); @@ -802,13 +802,14 @@ public: /** Wait indefinitely until the semaphore count becomes strictly positive and then decrement it and return. - + @returns One of: */ wxSemaError Wait(); }; + /** @class wxMutexLocker @wxheader{thread.h} @@ -823,8 +824,7 @@ public: @library{wxbase} @category{thread} - @seealso - wxMutex, wxCriticalSectionLocker + @see wxMutex, wxCriticalSectionLocker */ class wxMutexLocker { @@ -844,10 +844,11 @@ public: /** Returns @true if mutex was acquired in the constructor, @false otherwise. */ - bool IsOk(); + bool IsOk() const; }; + /** @class wxMutex @wxheader{thread.h} @@ -874,8 +875,7 @@ public: @library{wxbase} @category{thread} - @seealso - wxThread, wxCondition, wxMutexLocker, wxCriticalSection + @see wxThread, wxCondition, wxMutexLocker, wxCriticalSection */ class wxMutex { @@ -893,49 +893,74 @@ public: /** Locks the mutex object. This is equivalent to LockTimeout() with infinite timeout. - + @returns One of: */ wxMutexError Lock(); /** Try to lock the mutex object during the specified time interval. - + @returns One of: */ wxMutexError LockTimeout(unsigned long msec); /** Tries to lock the mutex object. If it can't, returns immediately with an error. - + @returns One of: */ wxMutexError TryLock(); /** Unlocks the mutex object. - + @returns One of: */ wxMutexError Unlock(); }; + // ============================================================================ // Global functions/macros // ============================================================================ +/** @ingroup group_funcmacro_thread */ +//@{ + /** - Returns @true if this thread is the main one. Always returns @true if - @c wxUSE_THREADS is 0. + This macro declares a (static) critical section object named @a cs if + @c wxUSE_THREADS is 1 and does nothing if it is 0. + + @header{wx/thread.h} */ -bool wxIsMainThread(); +#define wxCRIT_SECT_DECLARE(cs) + +/** + This macro declares a critical section object named @a cs if + @c wxUSE_THREADS is 1 and does nothing if it is 0. As it doesn't include + the @c static keyword (unlike wxCRIT_SECT_DECLARE()), it can be used to + declare a class or struct member which explains its name. + + @header{wx/thread.h} +*/ +#define wxCRIT_SECT_DECLARE_MEMBER(cs) + +/** + This macro creates a wxCriticalSectionLocker named @a name and associated + with the critical section @a cs if @c wxUSE_THREADS is 1 and does nothing + if it is 0. + + @header{wx/thread.h} +*/ +#define wxCRIT_SECT_LOCKER(name, cs) /** - This macro combines wxCRIT_SECT_DECLARE and - wxCRIT_SECT_LOCKER: it creates a static critical - section object and also the lock object associated with it. Because of this, it - can be only used inside a function, not at global scope. For example: + This macro combines wxCRIT_SECT_DECLARE() and wxCRIT_SECT_LOCKER(): it + creates a static critical section object and also the lock object + associated with it. Because of this, it can be only used inside a function, + not at global scope. For example: @code int IncCount() @@ -948,35 +973,56 @@ bool wxIsMainThread(); } @endcode - (note that we suppose that the function is called the first time from the main - thread so that the critical section object is initialized correctly by the time - other threads start calling it, if this is not the case this approach can - @b not be used and the critical section must be made a global instead). + Note that this example assumes that the function is called the first time + from the main thread so that the critical section object is initialized + correctly by the time other threads start calling it, if this is not the + case this approach can @b not be used and the critical section must be made + a global instead. + + @header{wx/thread.h} */ -#define wxCRITICAL_SECTION(name) /* implementation is private */ +#define wxCRITICAL_SECTION(name) /** - This macro declares a critical section object named @a cs if - @c wxUSE_THREADS is 1 and does nothing if it is 0. As it doesn't - include the @c static keyword (unlike - wxCRIT_SECT_DECLARE), it can be used to declare - a class or struct member which explains its name. + This macro is equivalent to + @ref wxCriticalSection::Leave "critical_section.Leave()" if + @c wxUSE_THREADS is 1 and does nothing if it is 0. + + @header{wx/thread.h} +*/ +#define wxLEAVE_CRIT_SECT(critical_section) + +/** + This macro is equivalent to + @ref wxCriticalSection::Enter "critical_section.Enter()" if + @c wxUSE_THREADS is 1 and does nothing if it is 0. + + @header{wx/thread.h} +*/ +#define wxENTER_CRIT_SECT(critical_section) + +/** + Returns @true if this thread is the main one. Always returns @true if + @c wxUSE_THREADS is 0. + + @header{wx/thread.h} */ -#define wxCRIT_SECT_DECLARE(cs) /* implementation is private */ +bool wxIsMainThread(); /** This function must be called when any thread other than the main GUI thread - wants to get access to the GUI library. This function will block the execution - of the calling thread until the main thread (or any other thread holding the - main GUI lock) leaves the GUI library and no other thread will enter the GUI - library until the calling thread calls ::wxMutexGuiLeave. + wants to get access to the GUI library. This function will block the + execution of the calling thread until the main thread (or any other thread + holding the main GUI lock) leaves the GUI library and no other thread will + enter the GUI library until the calling thread calls wxMutexGuiLeave(). + Typically, these functions are used like this: @code void MyThread::Foo(void) { - // before doing any GUI calls we must ensure that this thread is the only - // one doing it! + // before doing any GUI calls we must ensure that + // this thread is the only one doing it! wxMutexGuiEnter(); @@ -987,36 +1033,25 @@ bool wxIsMainThread(); } @endcode - Note that under GTK, no creation of top-level windows is allowed in any - thread but the main one. This function is only defined on platforms which support preemptive threads. + + @note Under GTK, no creation of top-level windows is allowed in any thread + but the main one. + + @header{wx/thread.h} */ void wxMutexGuiEnter(); /** - This macro declares a (static) critical section object named @a cs if - @c wxUSE_THREADS is 1 and does nothing if it is 0. -*/ -#define wxCRIT_SECT_DECLARE(cs) /* implementation is private */ + This function is only defined on platforms which support preemptive + threads. -/** - This macro is equivalent to @ref wxCriticalSection::leave cs.Leave if - @c wxUSE_THREADS is 1 and does nothing if it is 0. -*/ -#define wxLEAVE_CRIT_SECT(wxCriticalSection& cs) /* implementation is private */ + @see wxMutexGuiEnter() -/** - This macro creates a @ref overview_wxcriticalsectionlocker "critical section - lock" - object named @a name and associated with the critical section @a cs if - @c wxUSE_THREADS is 1 and does nothing if it is 0. + @header{wx/thread.h} */ -#define wxCRIT_SECT_LOCKER(name, cs) /* implementation is private */ +void wxMutexGuiLeave(); -/** - This macro is equivalent to @ref wxCriticalSection::enter cs.Enter if - @c wxUSE_THREADS is 1 and does nothing if it is 0. -*/ -#define wxENTER_CRIT_SECT(wxCriticalSection& cs) /* implementation is private */ +//@}