Click here to Skip to main content
15,914,594 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
General*** Saving TABLES from Access with MFC application *** Pin
Steve Lai5-Oct-00 11:47
Steve Lai5-Oct-00 11:47 
General *** Displaying TABLES from Access in a MFC application *** Pin
Steve Lai5-Oct-00 11:45
Steve Lai5-Oct-00 11:45 
Generalconnect to lan pc for management Pin
Tony5-Oct-00 8:41
Tony5-Oct-00 8:41 
QuestionHow to restore users Visual Studio profiles on WinNT4 Pin
adilo045-Oct-00 7:56
adilo045-Oct-00 7:56 
GeneralConvert BSTR into LPSTR Pin
gupta5-Oct-00 6:18
gupta5-Oct-00 6:18 
GeneralRe: Convert BSTR into LPSTR Pin
Michael Dunn5-Oct-00 7:19
sitebuilderMichael Dunn5-Oct-00 7:19 
QuestionHow to change the language in my application Pin
Anonymous5-Oct-00 3:09
Anonymous5-Oct-00 3:09 
QuestionHow to IMPLEMENT a Mutex in C++ ? Pin
GBO5-Oct-00 2:06
GBO5-Oct-00 2:06 
This looks like a school project, but it's not.

Having used mutexes and semaphores a zillion times, "finally the time has arrived to write my own implementation for use in an embedded system without OS." Frown | :-(

I did some research on the matter and I found Peterson's Algorithm (for 2 processes, threads or whatever) and Lamport's bakery algorithm (for N processes, threads or whatever). Wink | ;) There is no need to reinvent "hot water" again, an existing algorithm will do just fine.

For info,

Peterson's Algorithm:

int flag[2] = { FALSE, FALSE } // flag[i] indicates that Pi wants to
// enter its critical section.
int turn = 0; // turn indicates which process has
// priority in entering its critical
// section.

// mutexbegin:
flag[i] = TRUE;
turn = 1 - i;
while (flag[1 - i] && turn == 1 - i)
;

// mutexend:
flag[i] = FALSE;

Now Lamport's Bakery algorithm.

Assumptions:

NPROCS is the number of processes.

max(int *array) returns the maximum value in array.

Each process has a unique ID, so ties on the number chosen are broken by comparing IDs.

Replace i with the appropriate process ID.


// Global initialization:
int choosing[NPROCS] = { FALSE };
int number[NPROCS] = { 0 };

// mutexbegin:
choosing[i] = TRUE;
number[i] = max(number) + 1;
choosing[i] = FALSE;
for (j = 0; j < NPROCS; ++j)
{
while (choosing[j])
;

while (number[j] != 0 && (number[j] < number[i] ||
number[j] == number[i] && j < i) )
;
}

// mutexend:
number[i] = 0;

THE QUESTION:

in the line
number[i] = max(number) + 1;
we see the function 'max'. How do I interprete this?
I thought it meant the maximum of all the numbers in number[], but that does not seam to work.
Also, any suggestions on writing a
bool CPP_Mutex::TryEnter(unsigned int PNumber)
function ?

Any input or pointers greatly appreciated! Cool | :cool:

NOTE: I know this class produces what is called a "spinning lock", but that's OK.

I tested it with a dual processor machine with the following code:

#define NPROCS 2
#undef Max
#define Max(A,B) ((A)<(B)?(B):(A))

// Implementation of LAMPORTS Bakery Algorithm

class CPP_Mutex
{
public:
CPP_Mutex()
{
memset(m_nChoosing, 0, sizeof(int)*NPROCS);
memset(m_nNumber, 0, sizeof(int)*NPROCS);
}
~CPP_Mutex()
{

}
void Enter(unsigned int PNumber)
{
m_nChoosing[PNumber] = 1;
m_nNumber[PNumber] = MAX(m_nNumber) + 1;
m_nChoosing[PNumber] = 0;
for (unsigned int teller = 0; teller < NPROCS; teller++)
{
while (m_nChoosing[teller])
;
while ( m_nNumber[teller] != 0
&& ( m_nNumber[teller] < m_nNumber[PNumber] ||
m_nNumber[teller] == m_nNumber[PNumber] && teller < PNumber)
)
;
}
}
void Leave(unsigned int PNumber)
{
m_nNumber[PNumber] = 0;
}
protected:
int MAX(int* numbers)
{
int toReturn = 0;
for (unsigned int teller = 0; teller < NPROCS; teller++)
{
toReturn = Max(toReturn, *(numbers+teller));
}
return toReturn;
}
private:
int m_nChoosing[NPROCS];
int m_nNumber[NPROCS];
};

unsigned int value = 0;
unsigned int prevvalue = 0;
bool stop = false;

CPP_Mutex theTestMutex;

void IncreaseValue(unsigned int PNumber)
{
// if (theTestMutex.TryEnter(PNumber))
{
theTestMutex.Enter(PNumber);
prevvalue = value;
value++;
assert(prevvalue == value-1);
// cerr << PNumber << ":" << value << "\t";
theTestMutex.Leave(PNumber);
}
}

DWORD WINAPI ThreadProc( LPVOID lpParameter )
{
while (!stop)
{
IncreaseValue((unsigned int) lpParameter);
}
return 0;
}

int main(int argc, char* argv[])
{
HANDLE hThread1 = NULL;
HANDLE hThread2 = NULL;
DWORD dwThreadID1 = 0;
DWORD dwThreadID2 = 0;

hThread1 = ::CreateThread(NULL, 0, ThreadProc, (void*) 0, 0, &dwThreadID1);
hThread2 = ::CreateThread(NULL, 0, ThreadProc, (void*) 1, 0, &dwThreadID2);

WaitForSingleObject(hThread1, 60000);

stop = true;

WaitForSingleObject(hThread1, INFINITE);
WaitForSingleObject(hThread2, INFINITE);

CloseHandle(hThread1);
CloseHandle(hThread2);

return 0;
}
AnswerRe: How to IMPLEMENT a Mutex in C++ ? Pin
Erik Funkenbusch5-Oct-00 7:52
Erik Funkenbusch5-Oct-00 7:52 
GeneralUpdate: How to IMPLEMENT a Mutex in C++ ? (Assembler Guru required) Pin
GBO5-Oct-00 21:42
GBO5-Oct-00 21:42 
GeneralRe: Update: How to IMPLEMENT a Mutex in C++ ? (Assembler Guru required) Pin
GBO5-Oct-00 23:32
GBO5-Oct-00 23:32 
QuestionFace Recognition Software? Pin
Jeremy Davis4-Oct-00 23:19
Jeremy Davis4-Oct-00 23:19 
GeneralHelp with colors in MFC, damn I'm bad at painting :) Pin
Fredrik4-Oct-00 21:53
Fredrik4-Oct-00 21:53 
GeneralRe: Help with colors in MFC, damn I'm bad at painting :) Pin
Jeremy Davis4-Oct-00 22:13
Jeremy Davis4-Oct-00 22:13 
GeneralAnyone got any idea of how to do this? Pin
Fredrik5-Oct-00 21:34
Fredrik5-Oct-00 21:34 
GeneralRe: Anyone got any idea of how to do this? Pin
Jeremy Davis5-Oct-00 22:22
Jeremy Davis5-Oct-00 22:22 
QuestionHow to hide main dialog Pin
Crystal4-Oct-00 17:44
Crystal4-Oct-00 17:44 
AnswerRe: How to hide main dialog Pin
Sam Hobbs4-Oct-00 18:30
Sam Hobbs4-Oct-00 18:30 
GeneralSTL Collection Classes Pin
paulb4-Oct-00 17:19
paulb4-Oct-00 17:19 
GeneralRe: STL Collection Classes Pin
Erik Funkenbusch5-Oct-00 8:03
Erik Funkenbusch5-Oct-00 8:03 
GeneralRe: STL Collection Classes Pin
Erik Funkenbusch5-Oct-00 8:17
Erik Funkenbusch5-Oct-00 8:17 
GeneralRe: STL Collection Classes Pin
Norbert Muench6-Oct-00 3:29
sussNorbert Muench6-Oct-00 3:29 
GeneralCustomize Open File Dialog Pin
Farzad Bakhtiar4-Oct-00 17:01
sussFarzad Bakhtiar4-Oct-00 17:01 
GeneralToolBar Pin
kin4-Oct-00 16:21
kin4-Oct-00 16:21 
GeneralRe: ToolBar Pin
Member 12089655-Oct-00 11:45
Member 12089655-Oct-00 11:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.