473,669 Members | 2,471 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# calling simple C++/CLI library deadlock

Hi,
We're working in VS 2005, Team edition, if it makes any difference at
all (should be up-to-date and all that, but could not guarantee it is
100%).

We've implemented a simple generic wrapper parser under C++/CLI. It's
a basic project created under Visual C++/CLR - Class Library - and
it's pretty much what it is, just one /clr compiled class, fully CLR,
no C++ native types or processing, nothing, just managed all the way.
The reason whey we did it in the first place was the better handling
of generics, at least what we thought at the time - e.g. being able to
specify Enum as a constraint, using safe_cast to switch between the
types (as opposed to C# which requires boxing to e.g. cast generic T
to float) and so on. Basically, it's a generic class w/ static methods
which take value types or Enums and parse them (the strings that is -
parsing to specific values for each type). It has some more handling
specific to our case and error handling but it's merely one page and
nothing much in it (attaching an excerpt at the end).

At first it seemed to work just fine. But when we run it through some
heavy load and under multi-threading scenario it started to deadlock
(what seems to be a deadlock but could be nothing else - we're
catching all exceptions and handling any such possible issue which
could cause things to stop) regularly and repeatably.
Our app is feeding off of a stock-market data feed, thus it's quite
heavy and requires multiple threads processing things in the
background - but this part could not be simpler. On the threads side,
we have 2-3 main threads (up to 5 w/ worker threads), one receiving
the data (thru sockets, all is in C#) and others processing things in
the back + the GUI one. What's important is that this processing, done
in C++, is always from one thread, so there is no need to lock
anything there + it's just some 'static' one-off processing so no
'state' or any variables to protect really.
The problems start when we turn on the data-feed and a good deal of
data starts coming in - it works for a minute or so (which is
important to state, so it works and starts working) but then
regularly, like a clockwork, deadlocks somewhere inside C++ library -
and it's always different place, so 'smells' like a typical deadlock
scenario.

We tried tracing it, it's always somewhere inside C++ but nothing
particular to point to. Our best guess it's something to do w/
'global' calls back to CLR core libraries, maybe that String Alloc
which was mentioned or something. At one point it looked like a
safe_cast issue (where we used float% instead of float, we're passing
'by ref' to methods - but even w/ that 'fixed' it still locks
regularly, maybe a tad less often). We also investigated if it could
be the static initializer or something, read thru posts and
everything, but nothing helped.

Then, other thing is, when we copied the code back to C#, all works
fine and under pressure. We ran the thing for hours/days and no
problems if C# to C#. But if we switch over to C++/CLI it deadlocks
pretty soon, rarely ends up running for more then 5 minutes, that's
about max. And we've re-written everything so that it's almost exact
code here and there, no differences whatsoever. Also we removed any
'exotic' stuff, like calling our Logging libraries (C#) from inside C+
+/CLI or anything like it. Basically it's just dealing w/ core
libraries, like strings, Enum parsing, safe_cast here and there and
that's about all there is to it. That's why it is a bit strange.

Finally, we gave up on this approach, as we have little time to deal
w/ such things and w/ real development waiting ahead - so removed the C
++ part entirely and doing everything in C#, and all is ok, so we're
not overly keen on moving it back to C++.
But, being a long-time .NET programmer, and C++ before it for many
years, I'm just a bit curious about what's going on here and what
could be the issue. As, w/o having this resolved, I'd be very
reluctant on doing anything in C++/CLI, while on the other side I've
always been a fan of mixing things, do in C# what it does best, move
to C++ when you have to deal w/ performance intensive stuff and so on.
Also, expected much more from CLI version, after managed C++, thought
it'd be more mature and reliable.
So, in short, any ideas or anybody from Microsoft willing to answer
this and point me to possible/probable places or calls which could
cause this. Not really sure where to look for.

Thanks in advance,
Dragan

part of the code:
public ref class Parser
{
public:
generic <typename TField, typename TValue>
where TField : Enum
where TValue : value class
static FieldSetStatus Set(TValue% value, TField field, String^
strvalue, Context<TField> ^ context)
{
try
{
TValue novalue = TValue();

FieldSetStatus status = Parse<TValue>(v alue, novalue, strvalue);
return status;
//return ProcessStatus<T Field>(context, status, field, strvalue,
TValue::typeid->Name);
}
catch( Exception^ e )
{
return FieldSetStatus: :ParseError;
}
}

private:
generic <typename TValue>
where TValue : value class
static FieldSetStatus Parse(TValue% value, TValue novalue, String^
strvalue, String^ format, IFormatProvider ^ culture)
{
value = novalue;

if (strvalue == "Not Found")
return FieldSetStatus: :NotFound;

try
{
if (!String::IsNul lOrEmpty(strval ue))
{
Type^ type = TValue::typeid;

if( type->Equals(float:: typeid) )
return ParseFloat(safe _cast<float>(va lue), strvalue, format,
culture);
if( type->Equals(int::ty peid) )
return ParseInt32( safe_cast<int>( value), strvalue, format,
culture);
if( type->Equals(DateTim e::typeid) )
return ParseDate(safe_ cast<DateTime>( value), strvalue, format,
culture);
if( type->Equals(TimeSpa n::typeid) )
return ParseTime(safe_ cast<TimeSpan>( value), strvalue, format,
culture);

return FieldSetStatus: :ParseError;
}
return FieldSetStatus: :Empty;
}
catch( Exception^ e )
{
return FieldSetStatus: :ParseError;
}
}

static FieldSetStatus ParseFloat(floa t% value, String^ strvalue,
String^ format, IFormatProvider ^ culture)
{
if( Single::TryPars e( strvalue, value ) )
return FieldSetStatus: :Success;
else
return FieldSetStatus: :ParseError;
}
static FieldSetStatus ParseInt32(int% value, String^ strvalue,
String^ format, IFormatProvider ^ culture)
{
if( Int32::TryParse ( strvalue, value ) )
return FieldSetStatus: :Success;
else
return FieldSetStatus: :ParseError;
}
static FieldSetStatus ParseDate(DateT ime% value, String^ strvalue,
String^ format, IFormatProvider ^ culture)
{
if( format==nullptr )
format = "MM/dd/yyyy";
if (DateTime::TryP arseExact(strva lue, format, culture,
System::Globali zation::DateTim eStyles::None, value))
return FieldSetStatus: :Success;
else
return FieldSetStatus: :ParseError;
}
static FieldSetStatus ParseTime(TimeS pan% value, String^ strvalue,
String^ format, IFormatProvider ^ culture)
{
if( TimeSpan::TryPa rse( strvalue, value ) )
return FieldSetStatus: :Success;
else
return FieldSetStatus: :ParseError;
}

};

Jun 2 '07 #1
2 4239
We're working in VS 2005, Team edition, if it makes any difference at
all (should be up-to-date and all that, but could not guarantee it is
100%).
SP1?
So, in short, any ideas or anybody from Microsoft willing to answer
this and point me to possible/probable places or calls which could
cause this. Not really sure where to look for.
Could you also post a small example of a stresstest that triggers the
deadlock behavior?
That way I could try to see what's happening when it locks up.

--
Kind regards,
Bruno van Dooren MVP - VC++
http://msmvps.com/blogs/vanDooren
br************* *********@hotma il.com
Jun 4 '07 #2
....Hi, sorry for the delay,
>SP1?
Yes, the latest build, all ok on that side, I've rechecked, even
though I didn't reinstall everything, but that's just desperate.
>Could you also post a small example of a stresstest that triggers the deadlock behavior?
....well, I've been trying to find some time to make a small stress-
test, finally did, but I was unable to come up w/ something to repeat
the problem w/o using the data-feed itself (at least within the time
frame). Tried setting up a test feed and various things but
everything's working ok. When I switch things over to real-time data
it starts to break. And I know this from before, over years of
experience w/ stock-market live data, it's a beast, if there is
anything that could possibly go wrong it will - but it's very hard to
simulate the live-data.
My guess is it's something to do with the data itself which causes
some error of a kind that blocks the main thread (and the app that
deadlocks is using main thread to receive the data over sockets, so it
could be) - the only logical thing I could come up w/, even though I
don't have any reasonable explanation for it. And just to add (if I
didn't mention it), it's the socket in the main thread (built in
'managed sockets') that receives the data, while background thread
process it and calls the C++/CLI part which deadlocks.

So, to rephrase my question, within the realm of the code I've pasted
above (as that's effectively the test unit which deadlocks under real-
time feed data), is there anything within the 'system calls' (CLR that
is) which could cause such a behavior? What about that string buffer
issue I've read somewhere or something else?

Also, any recommendations on how to debug such an application, as
debugger deadlocks even faster so I'm unable to do anything but to
trace things, which I did and helps nothing. Is there any other way of
debugging such deadlocks (I know there were pdb-s of system libraries
for C++ so you could debug into it, anything similar for CLR?). As
much as I'd like you to debug it for me :) it's not what I had in
mind, rather to get some pointers on what could cause this and where
to look for, and how to debug it.

Thanks again

Jun 16 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2411
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. Generally, the documentation suggests that queues or similar constructs should be used for thread inter-process comms. I've had a lot of success in doing that (generally by passing in the queue during the __init__ of the thread) and I can see...
1
6067
by: Robert Brown | last post by:
I have a deadlock that's happening on one oracle instance but cannot be reproduced on any other. It is always caused by the same SQL statement colliding with itself and only happens under very high load. The statement is DELETE from userlogins WHERE numlogins <= 0 the schema for the userlogins table is
1
3995
by: dawatson833 | last post by:
I want to set an alert for a specific table whenever an event has caused a deadlock to occur on the table. I understand how to set up an alert. But I don't know which error number to use for the New Alert error number property for a deadlock. Or how to specify a deadlock on a specific table. Thanks, DW
3
7621
by: Nigel Robbins | last post by:
Hi There, I'm getting a deadlock when I have two clients running the following statement. DELETE FROM intermediate.file_os_details WHERE file_uid = ? AND obj_uid There is a compound index on file_uid / obj_uid. The isolation level is UR and I have set DB2_RR_TO_RS=YES. Any thoughts why I'm getting the deadlock ?
1
4236
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here. From the log it looks like the problem happens when 2 threads insert 1 record each in the same table and then try to aquire a NS (Next Key Share) lock on the record inserterd by the other thread. Thanks Rohit
0
1725
by: szehau | last post by:
Hi all, I have a program written in C with embeded SQL. Following are the configuration: DB2/LINUX 8.1.5 Thread model: posix gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) My problems are as following
5
3065
by: Gerhard Menzl | last post by:
When creating a Managed C++ DLL using the Visual Studio 7.1 Solution Explorer (by selecting Add New Project and then either choosing Class Library (.NET) or Windows Control Library (.NET)), the IDE sets the /MT(d) compiler switch (statically linked multithreaded C runtime library) by default. This is fine with me, as it relieves me from having to redistribute MSVCR71(D).DLL with my application. However, as soon as a single runtime...
2
8960
by: Sumanth | last post by:
Hi , I am trying to acquire a lock on a table A in exclusive mode, and this statement gives an error indicating a deadlock or timeout has been detected. The lock timeout value is set to 0 which I understand is to wait for however long it takes to acquire a lock. Also there are other processes that have acquired row level exclusive locks on the table A when this error happened.Is DB2 throwing this error as a pre-emptive measure.
0
11695
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to java.sql.SQLException: Deadlock found when trying to get lock; Try restarting transaction message from server: "Lock wait timeout exceeded; try restarting transaction"; We get such errors generally on inserts or updates while applying a
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8383
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8894
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8587
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2029
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.