1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: a sample console (as opposed to GUI) progam using wxWindows
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
22 #include <wx/string.h>
26 // ----------------------------------------------------------------------------
27 // conditional compilation
28 // ----------------------------------------------------------------------------
36 // ============================================================================
38 // ============================================================================
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
46 #include <wx/thread.h>
48 static size_t gs_counter
= (size_t)-1;
49 static wxCriticalSection gs_critsect
;
50 static wxCondition gs_cond
;
52 class MyJoinableThread
: public wxThread
55 MyJoinableThread(size_t n
) : wxThread(wxTHREAD_JOINABLE
)
56 { m_n
= n
; Create(); }
58 // thread execution starts here
59 virtual ExitCode
Entry();
65 wxThread::ExitCode
MyJoinableThread::Entry()
67 unsigned long res
= 1;
68 for ( size_t n
= 1; n
< m_n
; n
++ )
72 // it's a loooong calculation :-)
79 class MyDetachedThread
: public wxThread
82 MyDetachedThread(char ch
) { m_ch
= ch
; Create(); }
84 // thread execution starts here
85 virtual ExitCode
Entry();
88 virtual void OnExit();
94 wxThread::ExitCode
MyDetachedThread::Entry()
97 wxCriticalSectionLocker
lock(gs_critsect
);
98 if ( gs_counter
== (size_t)-1 )
104 static const size_t nIter
= 10;
105 for ( size_t n
= 0; n
< nIter
; n
++ )
113 wxThread::Sleep(100);
119 void MyDetachedThread::OnExit()
121 wxCriticalSectionLocker
lock(gs_critsect
);
126 #endif // TEST_THREADS
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
134 void PrintArray(const char* name
, const wxArrayString
& array
)
136 printf("Dump of the array '%s'\n", name
);
138 size_t nCount
= array
.GetCount();
139 for ( size_t n
= 0; n
< nCount
; n
++ )
141 printf("\t%s[%u] = '%s'\n", name
, n
, array
[n
].c_str());
145 #endif // TEST_ARRAYS
147 // ----------------------------------------------------------------------------
149 // ----------------------------------------------------------------------------
151 int main(int argc
, char **argv
)
153 if ( !wxInitialize() )
155 fprintf(stderr
, "Failed to initialize the wxWindows library, aborting.");
167 puts("*** Initially:");
169 PrintArray("a1", a1
);
171 wxArrayString
a2(a1
);
172 PrintArray("a2", a2
);
174 wxSortedArrayString
a3(a1
);
175 PrintArray("a3", a3
);
177 puts("*** After deleting a string from a1");
180 PrintArray("a1", a1
);
181 PrintArray("a2", a2
);
182 PrintArray("a3", a3
);
184 puts("*** After reassigning a1 to a2 and a3");
186 PrintArray("a2", a2
);
187 PrintArray("a3", a3
);
188 #endif // TEST_ARRAYS
192 for ( size_t n
= 0; n
< 8000; n
++ )
194 s
<< (char)('A' + (n
% 26));
198 msg
.Printf("A very very long message: '%s', the end!\n", s
.c_str());
200 // this one shouldn't be truncated
203 // but this one will because log functions use fixed size buffer
204 // (note that it doesn't need '\n' at the end neither - will be added
206 wxLogMessage("A very very long message 2: '%s', the end!", s
.c_str());
210 puts("Testing detached threads...");
212 static const size_t nThreads
= 3;
213 MyDetachedThread
*threads
[nThreads
];
215 for ( n
= 0; n
< nThreads
; n
++ )
217 threads
[n
] = new MyDetachedThread('A' + n
);
220 threads
[0]->SetPriority(WXTHREAD_MIN_PRIORITY
);
221 threads
[1]->SetPriority(WXTHREAD_MAX_PRIORITY
);
223 for ( n
= 0; n
< nThreads
; n
++ )
228 // wait until all threads terminate
234 puts("\n\nTesting a joinable thread used for a loooong calculation...");
236 // calc 10! in the background
237 MyJoinableThread
thread(10);
240 printf("\nThread terminated with exit code %lu.\n",
241 (unsigned long)thread
.Wait());
242 #endif // TEST_THREADS