Advertisement

10.09.2006 at 04:08AM PDT, ID: 22017538
[x]
Attachment Details

HowTo: Use Win32 critical-section calls?

Asked by newton-allan in C Programming Language

Tags: , , ,

I'm having problems getting the Win32 CriticalSection calls to work. In the following code, the call to EnterCriticalSection seems to be ignored, and each call to SearchFunc gets the value 'z', rather than the first getting 'A', the second getting 'a', and the third getting 'z'.

Am I doing something wrong, or leaving something out?

I was expecting that the matched pairs of EnterCriticalSection and LeaveCriticalSection would prevent the main thread from running past the _beginthreadex calls until each thread had a chance to get started. The threads should run for 5 seconds, with interspersed A's and a's and z's. Instead, all three threads seem to be created as if called with 'z'. Also, I'm not certain that three threads are created.

// EeTestMultiThread.cpp
#include "stdafx.h"
#include <process.h>

CRITICAL_SECTION gCriticalSection;
volatile DWORD   gTickCountStop;
#define  PROXY_LIMIT  50000000

UINT __stdcall SearchFunc(void* pArguments)
{
  UINT* pArgs = (UINT*)pArguments;
  UINT  curTransIndex = *pArgs;
  UINT  proxyFoundCount = 0;
  ::printf("Reached SearchFunc: %d\n", curTransIndex);
  ::LeaveCriticalSection(&gCriticalSection);
 
  while (TRUE) {
    UINT  curTickCount = ::GetTickCount();
    if (curTickCount > gTickCountStop) {
      break;
    }
    proxyFoundCount += curTransIndex;
    if (proxyFoundCount > PROXY_LIMIT) {
      printf("%c", curTransIndex);
      proxyFoundCount = 0;
    }
  }
  printf("Search: %d\n", proxyFoundCount);
  ::_endthreadex(0);
  return 0;
}

int main(void)
{
  ::InitializeCriticalSection(&gCriticalSection);
  gTickCountStop = (::GetTickCount() + 5000); // Run for 5 seconds
  UINT   threadID;
  HANDLE hThread[3];
  UINT   transIndex = 'A';
  ::EnterCriticalSection(&gCriticalSection);
  hThread[0] = (HANDLE)::_beginthreadex(NULL, 0, &SearchFunc,
                                        &transIndex, 0, &threadID);

  transIndex = 'a';
  ::EnterCriticalSection(&gCriticalSection);
  hThread[1] = (HANDLE)::_beginthreadex(NULL, 0, &SearchFunc,
                                        &transIndex, 0, &threadID);

  transIndex = 'z';
  ::EnterCriticalSection(&gCriticalSection);
  hThread[2] = (HANDLE)::_beginthreadex(NULL, 0, &SearchFunc,
                                        &transIndex, 0, &threadID);

  ::WaitForMultipleObjects(3, hThread, TRUE, INFINITE);
  ::DeleteCriticalSection(&gCriticalSection);
  return 0;
}


Start Free Trial
 
Loading Advertisement...
 
[+][-]10.09.2006 at 06:08AM PDT, ID: 17690329

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Tags: entercriticalsection, howto, use, waitformultipleobjects
Sign Up Now!
Solution Provided By: AlexFM
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.09.2006 at 07:17AM PDT, ID: 17690719

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.09.2006 at 07:44AM PDT, ID: 17690886

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.10.2006 at 03:21AM PDT, ID: 17697421

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32