RE: CRITICAL_SECTION failure on CE6

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I do not know about CE6.0 limits, but it sounds like you may be better off
doing the protection yourself, perhaps using low level interlocked
inc/dec/test&mod etc). Assuming the possibility to have a lock rejected is
low, it may benefit the performance as well. Problem may be the priority
inheritance (if you need it) and slower acquiring/releasing a lock after
contention etc. as you would need then to interact with the OS.

Michal

"Werner" wrote:

Hi,

Run this code:

const int COUNT = 100000;
CRITICAL_SECTION cs[COUNT];

int Initialize()
{
int i = 0;

SetLastError(0);
for (i = 0; i < COUNT; ++i)
{
InitializeCriticalSection(&cs[i]);
if (cs[i].hCrit == NULL)
{
DWORD e = GetLastError();
printf("failed at %d with %d\n", i, e);
break;
}
}
return i;
}

int Enter()
{
int i = 0;

SetLastError(0);
for (i = 0; i < COUNT; ++i)
{
EnterCriticalSection(&cs[i]);
if (cs[i].hCrit == NULL)
{
DWORD e = GetLastError();
printf("failed at %d with %d\n", i, e);
break;
}
}
return i;
}

Initialize();
Enter();

It fails after 65533 attempts when run on CE6. It runs fine on CE5.
I found out that the problem is not the CRITICAL_SECTION itself, but the
amount of HANDLES in use (underlying a CS uses a HANDLE). The problem is that
you can't know whether you reached the limit or not (though you can see this
on the hCrit member of the CS, but this isn't official documented. I found
out this behaviour by looking in the kernel source code)
No error what so ever is generated when the CS is not valid. Only if you do
eg CreateEvent() afterwards GetLastError() will return 14 (ERROR_OUTOFMEMORY).

I didn't find any documentation on the maximum allowed HANDLEs/ process in
CE6? Am I correct that it is limited to 65536 in CE6?
Is there a way to correctly create more CRITICAL_SECTIONS? I need to protect
several 10000s objects fro multithread access and I don't like to use shared
CSs as it might influence performance in my realtime app. Not to speak for
possible deadlocks that I might introduce.

Werner
.



Relevant Pages

  • Re: switch() Statement Question
    ... specify the limits in a more robust manner, but a number of very bright ... I'm not experienced in the art of compiler design; ... This is actually a serious suggestion, not an April Fool's joke. ... int main ...
    (comp.lang.c)
  • Re: switch() Statement Question
    ... specify the limits in a more robust manner, but a number of very bright ... I'm not experienced in the art of compiler design; ... This is actually a serious suggestion, not an April Fool's joke. ... int main ...
    (comp.lang.c)
  • Re: A generic interface for numeric variables
    ... There are many variables with the same limits, but it is not true for all. ... the size of variables may vary: int is 16-bits large for 16-bit machine and 32-bit large for 32-bit machine. ... long, or some upper limits are negative, then you need to treat ... built-in to set_variable (which will itself deal with signedness), ...
    (comp.lang.c)
  • Re: How do I avoid this corelated subquery?
    ... CREATE TABLE Limits ... prodid INT NOT NULL, ... CREATE TABLE Templimits ...
    (microsoft.public.sqlserver.programming)
  • Re: How do I avoid this corelated subquery?
    ... >> UPDATE Limits ... table to update, Limits, and therefore should be qualified accordingly, e.g., ... > prodid INT NOT NULL, ... > CREATE TABLE Templimits ...
    (microsoft.public.sqlserver.programming)