1999-10-06 13:48:34 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: samples/console/console.cpp
|
|
|
|
// Purpose: a sample console (as opposed to GUI) progam using wxWindows
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Modified by:
|
|
|
|
// Created: 04.10.99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <wx/string.h>
|
1999-10-07 14:12:57 -04:00
|
|
|
#include <wx/file.h>
|
1999-10-06 13:48:34 -04:00
|
|
|
#include <wx/app.h>
|
1999-10-07 14:12:57 -04:00
|
|
|
#include <wx/thread.h>
|
1999-10-06 13:48:34 -04:00
|
|
|
|
1999-10-07 14:12:57 -04:00
|
|
|
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)
|
1999-10-06 13:48:34 -04:00
|
|
|
{
|
|
|
|
if ( !wxInitialize() )
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Failed to initialize the wxWindows library, aborting.");
|
|
|
|
}
|
|
|
|
|
1999-10-07 14:12:57 -04:00
|
|
|
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!");
|
1999-10-06 13:48:34 -04:00
|
|
|
|
1999-10-07 14:12:57 -04:00
|
|
|
for ( n = 0; n < nThreads; n++ )
|
|
|
|
{
|
|
|
|
threads[n]->Delete();
|
|
|
|
}
|
1999-10-06 13:48:34 -04:00
|
|
|
|
|
|
|
wxUninitialize();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|