473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

do i need monitors and locking?

hi all,

im trying to establish whether i have a race condition or critical section
in the following. i have a dataaccess class that continually retireves a
table from a sqlserver (which may be slow). it provides a cached copy of
this table to clients, for them to read from (note: there are no writes).
the idea being if a particular table retrival takes time, the class would
still provide a cached copy.

now the code:

public abstract class DataAccess
{

protected DataTable cached;
protected int repeattime;

private DataSet ds;

public UpdatableDataAccess(string s, string db):base(s, db)

{

//create connection initialise dataset

}

protected DataTable fillDataSet(string tablename, SqlCommand sqlcom)

{

SqlDataAdapter mysda = new SqlDataAdapter(sqlcom);

if (ds.Tables.Contains(tablename))

{

ds.Tables[tablename].Clear();

}

sqlc.Open();

mysda.Fill(ds, tablename);

sqlc.Close();

return ds.Tables[tablename].Copy();

}

public void updateTable()

{

while(true)

{

SqlCommand sqlcom = new SqlCommand(updatequery, sqlc);

DataTable holding = fillDataSet(tablename, sqlcom);

//critical section, i think

cached = holding;

//end critical

Thread.Sleep(repeattime * 1000);

}

}

public DataTable getLatestTable()

{

//critical section, i think

return cached;

//end critical

}

}

have i indicated the correct critical sections? if so, do i need to lock /
tryenter around them? i assume that the copy assignment to the cached field
is atomic, so there shouldnt be any problem, right? clients would use
"getLatestTable()" to get a table, while the thread method would be
"updateTable()". this is my first real MT app, so i think im missing
something!

Spammy
Jul 21 '05 #1
2 1312
Hi all,

Further to my multithreading question from yesterday, it seems that,
although assignments are atomic, field values may be cached within threads
and so its still worth using locking to force reads from memory. However, I
also read that declaring a field as volatile would do this too (since
locking actaully uses volatile read and writes).

Its not much difference in work implementing either, but which is the best
solution? Volatile is said to have some unusual semantics, but im not sure
what these are. Locking OTOH, seems (to me at least) to be a bit of an
overkill for a simple assignment or read...

Thanks!

Spammy
Jul 21 '05 #2
spammy <me@privacy.net> wrote:
Further to my multithreading question from yesterday, it seems that,
although assignments are atomic, field values may be cached within threads
and so its still worth using locking to force reads from memory. However, I
also read that declaring a field as volatile would do this too (since
locking actaully uses volatile read and writes).
Yes it will.
Its not much difference in work implementing either, but which is the best
solution? Volatile is said to have some unusual semantics, but im not sure
what these are. Locking OTOH, seems (to me at least) to be a bit of an
overkill for a simple assignment or read...


Personally I use locks everywhere, and never bother with volatile. That
means that if I change to write more than one field, I don't have to
rething things. Likewise not all types can be used with volatile
variables.

If you put the locking code within property access/mutate, you don't
need to have locks all over the place in your code - just use the
property instead.

I would recommend *not* locking on "this"/"Me" though - you should
generally try to lock on a reference which other classes don't have
access to.

I'm writing a multi-threading article which covers a lot of this, but
I'm afraid it won't be ready for a while - it's a big topic!

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #3

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

Similar topics

2
by: datapro01 | last post by:
Running db2 8.1 on aix 5.1. We are testing some Quest database tools and have run into a situation where we keep getting the following error message when trying to use the SQL analysis tool. ...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
1
by: JMM | last post by:
I'm writing a control program that will require a lot of screen real estate to interact with the user. One thought I had on how to do this was to use a dual output display adapter and two...
2
by: spammy | last post by:
hi all, im trying to establish whether i have a race condition or critical section in the following. i have a dataaccess class that continually retireves a table from a sqlserver (which may be...
9
by: colin.mcnulty | last post by:
Hi, I'm a SQL Server DBA, but I guess that won't buy me any friends round here huh? ;-) I've been asked to look at the SQL that's being executed on a DB2 database from a web app, specifically...
4
by: Mathias Hasselmann | last post by:
Hello, Hope this is the right group for asking this. Didn't see a threading specific newsgroup here. I currently expire unexpected dead-locks within some trivial C# code. Now I wonder if it...
2
by: Cindy | last post by:
I've been using dual monitors for about a year now (absolutely love it), but have run into a weird situation with Access 2000 lately. I loaded a database on two PC's that do NOT have dual...
0
by: Ayushi Mehra | last post by:
Samsung SyncMaster 940 NW Monitors inch, LCD, Colour monitor consumes 38 watts of power per hour, has digital user controls and a maximum resolution of 1280x1024 dpi. The screen is flat with...
4
by: jonhmardona | last post by:
thanks for sharing guys ................. I am a student filmmaker and I have been looking at investing in higher end editing programs, but I like using dual monitors for that. I have heard it is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.