📄 step4.cpp
字号:
/*
* Step4: test sleeping threads.
*/
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include "PiAPI.h"
#include "TestOS.h"
#define NUM_LOOPS 10
#define NUM_THREADS 5
unsigned long thread_fn( unsigned long ulData )
{
int iSleepPeriod = (int)ulData;
PIThread *pThread = PIThread_getCurrent();
if ( !pThread )
{ FAILED(7); return 0L; };
for(int i=0; i<NUM_LOOPS; i++)
{
cout << "This is thread #" << (void *)pThread << " in loop " << i
<< ", sleeping for " << iSleepPeriod << "(ms)." << endl;
if ( PIPlatform_sleep( iSleepPeriod ) ) { FAILED(8); };
};
return 0;
}
int iRet;
void Step4()
{
cout << "Program running on " << PIPlatform_getDescription() << endl;
PIThread *aThreads[ NUM_THREADS ];
int iNumThreads = 0;
if ( PIThread_setPriority( PIThread_getCurrent(), PITHREAD_PRIORITY_HIGH ))
{ FAILED(2); };
int bOK = 1;
for( ; bOK && (iNumThreads < NUM_THREADS); iNumThreads++ )
{
PIThread *pThread = PIThread_new( 0, 0 );
if ( !pThread )
{ FAILED(8); };
if ( PIThread_begin( pThread, thread_fn,
(unsigned long)( (NUM_THREADS - iNumThreads) * 100 ),
PITHREAD_PRIORITY_MED, 0 ) )
{
cout << "Error starting thread" << endl;
FAILED(3);
iRet = -1; return;
};
if ( !pThread )
{
cout << "Error allocating thread " << endl;
FAILED(4);
bOK = 0;
}
else
{
aThreads[ iNumThreads ] = pThread;
};
};
PIThread_dbgDump( 0 );
/* --- wait for all threads to die (a natural death ) --- */
for( int i=0; i < iNumThreads; i++ )
{
PIThread *pThread = aThreads[i];
if ( PIThread_waitForThread( pThread ) )
{ FAILED(5); };
cout << (void *)pThread << " dead " << endl;
if ( PIThread_delete( pThread ) ) { FAILED(1); };
};
OK;
iRet = 0;
}
int main()
{
if ( PIPlatform_enter( "dummy", PIPLATFORM_VERSION_1_0, Step4 ) )
{
FAILED(6);
cout << "Platform_enter() failed." << endl;
exit( 1 );
};
return iRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -