X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3a5677401f4a490535ae8d0a28dc5aecd2b1ce30..c29c95fe24973b94fd724db767193171ca7c513d:/interface/wx/thread.h?ds=sidebyside diff --git a/interface/wx/thread.h b/interface/wx/thread.h index 0d27327b25..adfe774091 100644 --- a/interface/wx/thread.h +++ b/interface/wx/thread.h @@ -3,7 +3,7 @@ // Purpose: interface of all thread-related wxWidgets classes // Author: wxWidgets team // RCS-ID: $Id$ -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -309,14 +309,14 @@ public: char m_data[1024]; wxCriticalSection m_dataCS; // protects field above - DECLARE_EVENT_TABLE() + wxDECLARE_EVENT_TABLE(); }; wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_UPDATE, wxThreadEvent) - BEGIN_EVENT_TABLE(MyFrame, wxFrame) + wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_COMMAND(wxID_ANY, wxEVT_COMMAND_MYTHREAD_UPDATE, MyFrame::OnThreadUpdate) EVT_CLOSE(MyFrame::OnClose) - END_EVENT_TABLE() + wxEND_EVENT_TABLE() void MyFrame::DoStartALongTask() { @@ -355,7 +355,7 @@ public: download_chunk(buffer, 1024); // this takes time... { - // ensure noone reads m_data while we write it + // ensure no one reads m_data while we write it wxCriticalSectionLocker lock(m_dataCS); memcpy(m_data+offset, buffer, 1024); offset += 1024; @@ -454,6 +454,37 @@ public: */ virtual ExitCode Entry() = 0; + /** + Callback called by Delete() before actually deleting the thread. + + This function can be overridden by the derived class to perform some + specific task when the thread is gracefully destroyed. Notice that it + will be executed in the context of the thread that called Delete() and + not in this thread's context. + + TestDestroy() will be true for the thread before OnDelete() gets + executed. + + @since 2.9.2 + + @see OnKill() + */ + virtual void OnDelete(); + + /** + Callback called by Kill() before actually killing the thread. + + This function can be overridden by the derived class to perform some + specific task when the thread is terminated. Notice that it will be + executed in the context of the thread that called Kill() and not + in this thread's context. + + @since 2.9.2 + + @see OnDelete() + */ + virtual void OnKill(); + /** @deprecated Use CreateThread() instead. @@ -513,11 +544,15 @@ enum wxCriticalSectionType Finally, you should try to use wxCriticalSectionLocker class whenever possible instead of directly using wxCriticalSection for the same reasons - wxMutexLocker is preferrable to wxMutex - please see wxMutex for an example. + wxMutexLocker is preferable to wxMutex - please see wxMutex for an example. @library{wxbase} @category{threading} + @note Critical sections can be used before the wxWidgets library is fully + initialized. In particular, it's safe to create global + wxCriticalSection instances. + @see wxThread, wxCondition, wxCriticalSectionLocker */ class wxCriticalSection @@ -535,11 +570,17 @@ public: ~wxCriticalSection(); /** - Enter the critical section (same as locking a mutex). - + Enter the critical section (same as locking a mutex): if another thread + has already entered it, this call will block until the other thread + calls Leave(). There is no error return for this function. - After entering the critical section protecting some global - data the thread running in critical section may safely use/modify it. + + After entering the critical section protecting a data variable, + the thread running inside the critical section may safely use/modify it. + + Note that entering the same critical section twice or more from the same + thread doesn't result in a deadlock; in this case in fact this function will + immediately return. */ void Enter(); @@ -550,6 +591,46 @@ public: void Leave(); }; +/** + The possible thread wait types. + + @since 2.9.2 +*/ +enum wxThreadWait +{ + /** + No events are processed while waiting. + + This is the default under all platforms except for wxMSW. + */ + wxTHREAD_WAIT_BLOCK, + + /** + Yield for event dispatching while waiting. + + This flag is dangerous as it exposes the program using it to unexpected + reentrancies in the same way as calling wxYield() function does so you + are strongly advised to avoid its use and not wait for the thread + termination from the main (GUI) thread at all to avoid making your + application unresponsive. + + Also notice that this flag is not portable as it is only implemented in + wxMSW and simply ignored under the other platforms. + */ + wxTHREAD_WAIT_YIELD, + + /** + Default wait mode for wxThread::Wait() and wxThread::Delete(). + + For compatibility reasons, the default wait mode is currently + wxTHREAD_WAIT_YIELD if WXWIN_COMPATIBILITY_2_8 is defined (and it is + by default). However, as mentioned above, you're strongly encouraged to + not use wxTHREAD_WAIT_YIELD and pass wxTHREAD_WAIT_BLOCK to wxThread + method explicitly. + */ + wxTHREAD_WAIT_DEFAULT = wxTHREAD_WAIT_YIELD +}; + /** The possible thread kinds. */ @@ -616,10 +697,10 @@ enum @section thread_types Types of wxThreads There are two types of threads in wxWidgets: @e detached and @e joinable, - modeled after the the POSIX thread API. This is different from the Win32 API + modeled after the POSIX thread API. This is different from the Win32 API where all threads are joinable. - By default wxThreads in wxWidgets use the @b detached behavior. + By default wxThreads in wxWidgets use the @b detached behaviour. Detached threads delete themselves once they have completed, either by themselves when they complete processing or through a call to Delete(), and thus @b must be created on the heap (through the new operator, for example). @@ -677,15 +758,15 @@ enum MyThread *m_pThread; wxCriticalSection m_pThreadCS; // protects the m_pThread pointer - DECLARE_EVENT_TABLE() + wxDECLARE_EVENT_TABLE(); }; - BEGIN_EVENT_TABLE(MyFrame, wxFrame) + wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_CLOSE(MyFrame::OnClose) EVT_MENU(Minimal_Start, MyFrame::DoStartThread) EVT_COMMAND(wxID_ANY, wxEVT_COMMAND_MYTHREAD_UPDATE, MyFrame::OnThreadUpdate) EVT_COMMAND(wxID_ANY, wxEVT_COMMAND_MYTHREAD_COMPLETED, MyFrame::OnThreadCompletion) - END_EVENT_TABLE() + wxEND_EVENT_TABLE() wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_COMPLETED, wxThreadEvent) wxDEFINE_EVENT(wxEVT_COMMAND_MYTHREAD_UPDATE, wxThreadEvent) @@ -778,7 +859,7 @@ enum if (m_pThread) // does the thread still exist? { - m_out.Printf("MYFRAME: deleting thread"); + wxMessageOutputDebug().Printf("MYFRAME: deleting thread"); if (m_pThread->Delete() != wxTHREAD_NO_ERROR ) wxLogError("Can't delete the thread!"); @@ -876,10 +957,10 @@ enum A common problem users experience with wxThread is that in their main thread they will check the thread every now and then to see if it has ended through IsRunning(), only to find that their application has run into problems - because the thread is using the default behavior (i.e. it's @b detached) and + because the thread is using the default behaviour (i.e. it's @b detached) and has already deleted itself. Naturally, they instead attempt to use joinable threads in place of the previous - behavior. However, polling a wxThread for when it has ended is in general a + behaviour. However, polling a wxThread for when it has ended is in general a bad idea - 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. @@ -946,7 +1027,7 @@ public: performance issues on those systems with small default stack since those typically use fully committed memory for the stack. On the contrary, if you use a lot of threads (say several hundred), - virtual adress space can get tight unless you explicitly specify a + virtual address space can get tight unless you explicitly specify a smaller amount of thread stack space for each thread. @return One of: @@ -960,6 +1041,15 @@ public: Calling Delete() gracefully terminates a @b detached thread, either when the thread calls TestDestroy() or when it finishes processing. + @param rc + The thread exit code, if rc is not NULL. + + @param waitMode + As described in wxThreadWait documentation, wxTHREAD_WAIT_BLOCK + should be used as the wait mode even although currently + wxTHREAD_WAIT_YIELD is for compatibility reasons. This parameter is + new in wxWidgets 2.9.2. + @note This function works on a joinable thread but in that case makes the TestDestroy() function of the thread return @true and then @@ -968,7 +1058,8 @@ public: See @ref thread_deletion for a broader explanation of this routine. */ - wxThreadError Delete(void** rc = NULL); + wxThreadError Delete(ExitCode *rc = NULL, + wxThreadWait waitMode = wxTHREAD_WAIT_BLOCK); /** Returns the number of system CPUs or -1 if the value is unknown. @@ -983,7 +1074,10 @@ public: /** Returns the platform specific thread ID of the current thread as a long. + This can be used to uniquely identify threads, even if they are not wxThreads. + + @see GetMainId() */ static wxThreadIdType GetCurrentId(); @@ -1001,6 +1095,15 @@ public: */ wxThreadKind GetKind() const; + /** + Returns the thread ID of the main thread. + + @see IsMain() + + @since 2.9.1 + */ + static wxThreadIdType GetMainId(); + /** Gets the priority of the thread, between zero and 100. @@ -1029,6 +1132,11 @@ public: /** Returns @true if the calling thread is the main application thread. + + Main thread in the context of wxWidgets is the one which initialized + the library. + + @see GetMainId(), GetCurrentId() */ static bool IsMain(); @@ -1098,6 +1206,10 @@ public: of detached threads. This function can only be called from another thread context. + + Finally, note that once a thread has completed and its Entry() function + returns, you cannot call Run() on it again (an assert will fail in debug + builds or @c wxTHREAD_RUNNING will be returned in release builds). */ wxThreadError Run(); @@ -1162,9 +1274,15 @@ public: This function can only be called from another thread context. + @param waitMode + As described in wxThreadWait documentation, wxTHREAD_WAIT_BLOCK + should be used as the wait mode even although currently + wxTHREAD_WAIT_YIELD is for compatibility reasons. This parameter is + new in wxWidgets 2.9.2. + See @ref thread_deletion for a broader explanation of this routine. */ - ExitCode Wait(); + ExitCode Wait(wxThreadWait flags = wxTHREAD_WAIT_BLOCK); /** Give the rest of the thread's time-slice to the system allowing the other @@ -1506,6 +1624,9 @@ public: Locks the mutex object. This is equivalent to LockTimeout() with infinite timeout. + Note that if this mutex is already locked by the caller thread, + this function doesn't block but rather immediately returns. + @return One of: @c wxMUTEX_NO_ERROR, @c wxMUTEX_DEAD_LOCK. */ wxMutexError Lock();