HI
I am learning multi-threaded programming.
I have written the following code to synchronize access to a common array by two different threads. One thread to fill values in the array while the other one reads values from that array.
The array is placed as member of a class and access to that array is provided via two member functions FillArray and ReadArray
The program displays error during run time.

I am unable to find out what is wrong with the following code while using CSingleLock object.
The same code appears to work fine when I use CCriticalSection object.
Someone please help me.

Thanks
Yuvaraj

threadsafearray.h
---------------------------------------------------------------------------------------------------------------------------

#include <afxwin.h>
#include <afxmt.h>

#ifndef __THREAD_SAFE_ARRAY_H__
#define __THREAD_SAFE_ARRAY_H__

#ifndef SZ_ARRAY
#define SZ_ARRAY (100)
#endif

class T_ThSafeArray
{
public:
T_ThSafeArray():SingleLock(&cs)
{
for(int i=0; i< SZ_ARRAY; i++)
{
Array[i]=0;
}
}

~T_ThSafeArray()
{
}

void FillArray(char arrVal[]); //*** Copies szElements no of elements from arrVal to Array
void ReadArray(char arrVal[]); //*** Reads from Array

private:
// data
char Array[SZ_ARRAY];

// Synchronization objects
CCriticalSection cs;
CSingleLock SingleLock;

};
#endif



threadsafearray.cpp
---------------------------------------------------------------------------------------------------------------------------
#include "threadsafearray.h"

#ifndef __THREAD_SAFE_ARRAY_CPP__
#define __THREAD_SAFE_ARRAY_CPP__

void T_ThSafeArray::FillArray(char arFillVal[])
{
SingleLock.Lock(); // cs.Lock();

if(SingleLock.IsLocked())
{
printf("\n Locked resources from fill array");

for(int i=0; i< SZ_ARRAY; i++)
{
Array[i] = arFillVal[i];
}

SingleLock.Unlock(); // cs.Unlock();
printf("\n Released resources from fill array");
}
else
{
printf("\n No lock formed, reporting from fill array");
}
}

void T_ThSafeArray::ReadArray(char arCopiedVal[])
{
//
SingleLock.Lock(); // cs.Lock();
// 
if(SingleLock.IsLocked())
{
printf("\n Locked resources from Read array");
for(int i=0; i< SZ_ARRAY; i++)
{
arCopiedVal[i] = Array[i];
}
// 
SingleLock.Unlock(); // cs.Unlock();
printf("\n Released resources from Read array");
}
else
{
printf("\n No lock formed reporting from Read array");
}

}

#endif

mt_mfc.cpp
---------------------------------------------------------------------------------------------------------------------------
// mt_mfc.cpp : Defines the entry point for the console application.

#include "stdafx.h"
const int ITER_MAX = 2;//000;

UINT __cdecl consumer(void * pArgs)
{
T_ThSafeArray &a = *((T_ThSafeArray*)pArgs);
char arOfValues[SZ_ARRAY];

int iter_count = 0;
for(; iter_count < ITER_MAX; iter_count++)
{
a.ReadArray(arOfValues);
for(int i= 0; i< SZ_ARRAY; i++)
{
// printf("\n%d", arOfValues[i]);
}
}
printf("Read Array had been called %d times",iter_count);
return 0;
}

UINT __cdecl producer(void * pArgs)
{
T_ThSafeArray &a = *((T_ThSafeArray*) pArgs);

char arOfValues[SZ_ARRAY];

int iter_count = 0;
for(; iter_count < ITER_MAX; iter_count++)
{
for(int i= 0; i< SZ_ARRAY; i++)
{
arOfValues[i]= i;
}
a.FillArray(arOfValues);
}
printf("Fill Array had been called %d times",iter_count);
return 0;
}



int _tmain(int argc, _TCHAR* argv[])
{
T_ThSafeArray a1, a2;
HANDLE hThread[2];

try
{
CWinThread* pThread1 = AfxBeginThread (consumer, LPVOID(&a1));
CWinThread* pThread2 = AfxBeginThread (producer, LPVOID(&a1));

if(pThread2 == NULL)
{
printf("\nError while creating the thread that producer that inturn uses mem func FILL ARRAY");
}

hThread[0]= pThread1->m_hThread;
hThread[1]= pThread2->m_hThread;

// WaitForSingleObject(hThread[0], INFINITE);
// WaitForSingleObject(hThread[1], INFINITE);

WaitForMultipleObjects(2, hThread, TRUE, INFINITE);
}
catch(...)
{
printf("\nSome Exception has occured");
exit(1);
}

return 0;
}

IMHO ditch that crappy CSingleLock class and use CriticalSection since it works for you the way you want it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.