X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3766781218689ca38a07fad5c73735317797c572..280132e34eed4d6be04f1b961bb6c9e844c97f35:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index e90e7b5858..804577456c 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -12,18 +12,95 @@ #include #include +#include #include +#include -int main() +static size_t gs_counter = (size_t)-1; +static wxCriticalSection gs_critsect; + +class MyThread : public wxThread +{ +public: + MyThread(char ch); + + // thread execution starts here + virtual void *Entry(); + + // and stops here + virtual void OnExit(); + +public: + char m_ch; +}; + +MyThread::MyThread(char ch) +{ + m_ch = ch; + + Create(); +} + +void *MyThread::Entry() +{ + { + wxCriticalSectionLocker lock(gs_critsect); + if ( gs_counter == (size_t)-1 ) + gs_counter = 1; + else + gs_counter++; + } + + for ( size_t n = 0; n < 10; n++ ) + { + if ( TestDestroy() ) + break; + + putchar(m_ch); + fflush(stdout); + + wxThread::Sleep(100); + } + + return NULL; +} + +void MyThread::OnExit() +{ + wxCriticalSectionLocker lock(gs_critsect); + gs_counter--; +} + +int main(int argc, char **argv) { if ( !wxInitialize() ) { fprintf(stderr, "Failed to initialize the wxWindows library, aborting."); } - wxString s("Hello, "); + static const size_t nThreads = 3; + MyThread *threads[nThreads]; + size_t n; + for ( n = 0; n < nThreads; n++ ) + { + threads[n] = new MyThread('+' + n); + threads[n]->Run(); + } + + // wait until all threads terminate + for ( ;; ) + { + wxCriticalSectionLocker lock(gs_critsect); + if ( !gs_counter ) + break; + } + + puts("\nThat's all, folks!"); - wxLogMessage(s + "world!"); + for ( n = 0; n < nThreads; n++ ) + { + threads[n]->Delete(); + } wxUninitialize();