📄 thread.cpp
字号:
// thread.cpp
//
// Copyright (c) 1999-2007 Symbian Software Ltd. All rights reserved.
//
// $Change: 937687 $
// This exercise is designed to build familiarity with threads,
// multi threading and use of critical sections.
//
// Firstly you will perform some basic thread operations
// and observe the behaviour. After that, you are asked to run
// the same code without implementation of the critical section.
// This will demonstrate how critical sections are used to serialize
// access to shared resources.
// SYSTEM HEADERS
#include <e32math.h>
// PROJECT HEADERS
#include "eustd.h"
// Common literal text
_LIT(KThread, "Threads are being executed \nnow ...\n\n");
_LIT(KArray, "Array contents:\n\n");
_LIT(KThread1, "threadWrite1");
_LIT(KThread2, "threadWrite2");
_LIT(KLastIntFormatString, "%d\n");
_LIT(KIntFormatString, "%d ");
// Global and Constant Variables
const TInt KHeapSize = 0x2000;
const TInt KMaxArraySize = 100;
TInt GlobalArray[KMaxArraySize];
TInt GlobalIndex = 0;
RCriticalSection criticalSection;
// Implementation of Write1 function used for threadWrite1
TInt Write1ThreadEntryPoint(TAny*)
{
TInt count = 0; //counter that resets at 10
// comment out the next line of code for the optional exercise (2).
criticalSection.Wait();
// write some 1s
while (GlobalIndex<KMaxArraySize)
{
GlobalArray[GlobalIndex] = 1;
GlobalIndex++;
// take a breather every 10 writes
if (count < 9)
{
count++;
}
else
{
User::After(1); // force context switch
count = 0;
}
}
// comment out the next line of code for the optional exercise (2).
criticalSection.Signal();
return(KErrNone);
} // end Write1ThreadEntryPoint
// Implementation of Write2 function used for threadWrite2
TInt Write2ThreadEntryPoint(TAny*)
{
TInt count = 0;
// comment out the next line of code for the optional exercise (2).
criticalSection.Wait();
// write some 2s
while(GlobalIndex < KMaxArraySize)
{
GlobalArray[GlobalIndex] = 2;
GlobalIndex++;
if (count < 9)
{
count ++;
}
else
{
User::After(1); // force context switch
count = 0;
}
}
// comment out the next line of code for the optional exercise (2).
criticalSection.Signal();
return(KErrNone);
} // end Write2ThreadEntryPoint
// Do the example
LOCAL_C void doExampleL()
{
// Local Variables
TInt ret;
RThread threadWrite1, threadWrite2;
TRequestStatus statusWrite1, statusWrite2;
// Create an open the handle to this critical section
ret = criticalSection.CreateLocal();
if (ret == KErrNone)
{
// (1) Complete the tasks described below to complete exercise (1).
// Create two threads using Create function.
// For Stack and Heap size use already defined constants
// KDefaultStackSize and KHeapSize and local variables.
// Names can be found above in 'Common literal text' section.
// Thread entry point functions are defined above.
threadWrite1.Create(KThread1, Write1ThreadEntryPoint, KDefaultStackSize, KHeapSize, KHeapSize, NULL);
threadWrite2.Create(KThread2, Write2ThreadEntryPoint, KDefaultStackSize, KHeapSize, KHeapSize, NULL);
// Request notification when these threads die using Logon
// function.
threadWrite1.Logon(statusWrite1);
threadWrite2.Logon(statusWrite2);
// Make both threads eligible for execution using Resume
// function.
threadWrite1.Resume();
threadWrite2.Resume();
// Print information
console->Printf(KThread);
// Wait for the above requests to complete. Use
// WaitForRequest of the User class. For more information
// check the SDK.
User::WaitForRequest(statusWrite1);
User::WaitForRequest(statusWrite2);
// Print elements of the array.
TInt col;
for (TInt pos = 0; pos < KMaxArraySize - 10; )
{
for (col = 0; col < 9; col++)
{
console->Printf(KIntFormatString,GlobalArray[pos++]);
}
console->Printf(KLastIntFormatString,GlobalArray[pos++]);
}
// End threads. Use Kill function.
threadWrite1.Kill(KErrNone);
threadWrite2.Kill(KErrNone);
// Close the handle to threads
threadWrite1.Close();
threadWrite2.Close();
// Close the handle to the critical section object.
criticalSection.Close();
}
} // end doExampleL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -