]> git.saurik.com Git - wxWidgets.git/blob - samples/console/console.cpp
1. wxLog::FlushActive() added
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include <stdio.h>
13
14 #include <wx/string.h>
15 #include <wx/file.h>
16 #include <wx/app.h>
17 #include <wx/thread.h>
18
19 static size_t gs_counter = (size_t)-1;
20 static wxCriticalSection gs_critsect;
21
22 class MyThread : public wxThread
23 {
24 public:
25 MyThread(char ch);
26
27 // thread execution starts here
28 virtual void *Entry();
29
30 // and stops here
31 virtual void OnExit();
32
33 public:
34 char m_ch;
35 };
36
37 MyThread::MyThread(char ch)
38 {
39 m_ch = ch;
40
41 Create();
42 }
43
44 void *MyThread::Entry()
45 {
46 {
47 wxCriticalSectionLocker lock(gs_critsect);
48 if ( gs_counter == (size_t)-1 )
49 gs_counter = 1;
50 else
51 gs_counter++;
52 }
53
54 for ( size_t n = 0; n < 10; n++ )
55 {
56 if ( TestDestroy() )
57 break;
58
59 putchar(m_ch);
60 fflush(stdout);
61
62 wxThread::Sleep(100);
63 }
64
65 return NULL;
66 }
67
68 void MyThread::OnExit()
69 {
70 wxCriticalSectionLocker lock(gs_critsect);
71 gs_counter--;
72 }
73
74 int main(int argc, char **argv)
75 {
76 if ( !wxInitialize() )
77 {
78 fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
79 }
80
81 static const size_t nThreads = 3;
82 MyThread *threads[nThreads];
83 size_t n;
84 for ( n = 0; n < nThreads; n++ )
85 {
86 threads[n] = new MyThread('+' + n);
87 threads[n]->Run();
88 }
89
90 // wait until all threads terminate
91 for ( ;; )
92 {
93 wxCriticalSectionLocker lock(gs_critsect);
94 if ( !gs_counter )
95 break;
96 }
97
98 puts("\nThat's all, folks!");
99
100 for ( n = 0; n < nThreads; n++ )
101 {
102 threads[n]->Delete();
103 }
104
105 wxUninitialize();
106
107 return 0;
108 }