Critical section ?

From: James (you_at_yo.net)
Date: 12/20/04


Date: Mon, 20 Dec 2004 21:43:59 GMT

I recently was asked to answer this

int g_nNums[100]

DWORD WINAPI Thread(LPVOID lpvParams)
{
    int iIndex = some value;

    EnterCriticalSection(g_CriticalSection);

    if( g_Nums[nIndex] < MIN_VAL)
        IncrementIndex(iIndex);
    else
        g_nNums[iIndex] = MIN_VAL;

    LeaveCriticalSecton(&g_CriticalSection);

    return 0;
}

void IncrementIndex(int iIndex)
{
...
    g_nNums[nIndex++];
...
}

How can IncrementIndex be written so that it is thread safe and won't
deadlock?

I answered that I would wrap it in a critical seciton or a mutex. What are
the potential problems with that answer ?

Thanks.



Relevant Pages

  • Critical section ?
    ... DWORD WINAPI Thread(LPVOID lpvParams) ... void IncrementIndex(int iIndex) ... How can IncrementIndex be written so that it is thread safe and won't ...
    (microsoft.public.vc.mfc)
  • critical section ?
    ... DWORD WINAPI Thread(LPVOID lpvParams) ... void IncrementIndex(int iIndex) ... How can IncrementIndex be written so that it is thread safe and won't ...
    (comp.programming)
  • Re: [OT] Critical section ?
    ... > void IncrementIndex(int iIndex) ... > How can IncrementIndex be written so that it is thread safe and won't ... need to take any additional steps to make it thread safe. ... as the above CS) that will protected only one element of the array - the ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Critical section ?
    ... > Jason wrote: ... >> void IncrementIndex ... > You have used both nIndex and iIndex in ambiguous way, ...
    (microsoft.public.vc.mfc)
  • Re: Critical section ?
    ... > void IncrementIndex ... You have used both nIndex and iIndex in ambiguous way, ... Shouldn't "int iIndex" actually be "int &iIndex"? ...
    (microsoft.public.vc.mfc)