[ Home | Programming Tips | Mail ]
PowerPlantは各 classの独立性が高いため LThread classと ANSI Consoleを組み合わせた Applicationなんてのも簡単にできます。
2つの Threadで MutexSemaphoreを取り合う Threaded ANSI Console Applicationの Project Fileをこちらに置いておきます。
ANSI_Thread.hqx
#include <stdio.h> // ANSI
#include <SIOUX.h> // Console for Mac
#include <LThread.h> // PowerPlant
#include <LMutexSemaphore.h>
#include <UEnvironment.h>
#include <UThread.h>
static LMutexSemaphore sMutexSema;
class CTestThread : public LThread {
protected:
const char *mMsg;
Int32 mSleep;
public:
CTestThread(const char *msg,Int32 sleep)
: LThread(false),mMsg(msg),mSleep(sleep) {}
virtual ~CTestThread() {}
protected:
virtual void *Run() {
for (;;) {
StMutex mutex(sMutexSema);
printf("%s grabbed the semaphore!\n",mMsg);
Sleep(mSleep);
}
return(nil);
}
};
static void EventLoop()
{
extern Boolean SIOUXQuitting;
const QHdr *qEvt = LMGetEventQueue();
while ( !SIOUXQuitting ) {
EventRecord event;
const long sleep = (0 < LThread::CountReadyThreads()) ? 0 : LMGetCaretTime();
const Boolean gotEvent = WaitNextEvent(everyEvent,&event,sleep,nil);
if ( !gotEvent ) LThread::Yield();
SIOUXHandleOneEvent(&event);
const Uint32 limitTick = LMGetTicks() + 2;
do {
LThread::Yield();
}while ( (qEvt->qHead == nil)
&& (0 < LThread::CountReadyThreads())
&& (LMGetTicks() < limitTick));
}
}
void main()
{
SIOUXSettings.autocloseonquit = false;
SIOUXSettings.asktosaveonclose = false;
printf("\n"); // Initialize Toolbox
// Check Thread Manager Availability
if ( !UEnvironment::HasFeature(env_HasThreadsManager) ) return;
new UMainThread; // Create Main Thread
CTestThread *thread1 = new CTestThread("Thread1",1000);
CTestThread *thread2 = new CTestThread("Thread2",2000);
thread1->Resume();
thread2->Resume();
EventLoop();
thread1->DeleteThread();
thread2->DeleteThread();
}
この Pageは MacOS X + Radio UserLand で作っています。