Should I pass a SqlDataReader by reference or not when passing it out to multiple threads.

Posted by deroby on Stack Overflow See other posts from Stack Overflow or by deroby
Published on 2010-06-03T14:01:57Z Indexed on 2010/06/03 14:04 UTC
Read the original article Hit count: 237

Filed under:
|

Hi all,

being new to c# I've run into this 'conundrum' when passing around a SqlDataReader between different threads. Without going into too much detail, the idea is to have a main thread fetching data from the database (a large recordset) and then have a helper-task run through this record by record and doing some stuff based upon the contents of this. There is no feedback to the recordset, it simply wades through until no records are left. This works fine, but given the nature of the job at hand it should be possible to have this job spread over different threads (CPUs) to maximize throughput (the order of execution is of no significance). The question then becomes, when I pass this recordset in a SqlDataReader, do I have to use ref or not ? It kind of boils down to the question : if I pass the object around without specifying ref, won't it create new copies in memory and have records processed n times ? Or, don't I risk having the record-position being moved forward while not all fields have been fully read yet ? The latter seems more like a 'data racing' issue and probably is covered by the lock()ing mechanism (or not?).

My initial take on the problem was that it doesn't really hurt passing the variable using ref, yet as a colleague put it : "you only need ref when you're doing something wrong" =) Additionally using ref restricts me from applying a Using() construction too which isn't very nice either. I thus create a "basic" project that tackles the same approach but without the ref notation. Tests so far show that it works flawlessly on a Core2Duo (2cpu) using any number of threads, yet I'm still a bit wary... What do you experts think about this ? Use ref or not ?

You can find the test-project here as it seems I can't upload it to this question directly ?!?

ps: it's just a test-project and I'm new to c#, so please be gentle on me when breaking down the code =P

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading