📄 step2.cpp
字号:
/*
* Step2: Initialize the platform, create a mutex. Lock and unlock
* the mutex. Wait for thread to terminate. Delete both objects and
* exit.
*/
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include "PiAPI.h"
#include "TestOS.h"
/* ---
Thread function
--- */
unsigned long thread_fn( unsigned long ulData )
{
PIThread *pThread = PIThread_getCurrent();
cout << "This is thread #" << (int)PIThread_getSystemHandle( pThread )
<< " with data " << ulData
<< endl;
return 0;
}
/* ---
The main function
--- */
int iRet;
void Step2()
{
PISync *pSync = 0;
PIThread *pThread = 0;
cout << "Program running on " << PIPlatform_getDescription() << endl;
if ( !( pSync = PIPlatform_allocLocalMutex() ) )
{
cout << "Unable to allocate mutex" << endl;
FAILED(1);
goto done;
};
if ( PISync_lock( pSync ) ) { FAILED(7); return; };
cout << "Got mutex lock" << endl;
if ( PISync_unlock( pSync ) ) { FAILED(6); return; };
cout << "Released mutex lock" << endl;
pThread = PIThread_new( 0, 0 );
if ( !pThread )
{
cout << "Unable to allocate thread object" << endl;
FAILED(2);
goto done;
};
if ( PIThread_begin( pThread, thread_fn, 0, PITHREAD_PRIORITY_MED, 0 ))
{
cout << "Unable to begin thread" << endl;
FAILED(8);
goto done;
};
if ( PIThread_waitForThread( pThread ) ) { FAILED(5); };
if ( pThread )
{
if ( PIThread_delete( pThread ) ) { FAILED(3); };
};
if ( pSync )
{
if ( PISync_delete( pSync ) ) { FAILED(4); };
};
done:
OK;
iRet = 0;
}
/* ---
Main function
--- */
int main()
{
if ( PIPlatform_enter( "dummy", PIPLATFORM_VERSION_1_0, Step2 ) )
{
cout << "Platform_enter() failed." << endl;
exit( 1 );
};
return iRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -