473,544 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optimistic locking

I have global variable which is being shared between threads (problem
is not connected with thread). Without using any mutex i have do some
operation global variable in *consistent* way.
consider following code :

int i = 0

fun (int j)
{

i = i + j ; i = i * somefun(i,j) ; /*i meant here is, we are doing
lot of operations */
}
above func is being executed by threads, We can change the
implementation of func .however , we need to maintain what that fun
does on that variable.
How can we implement the concept of optimistic locking here. I got lot
defination of optimistic locking on internet .

But i couldnt find any implementation. So i thought of implementing one
small programme . Can we apply concept of optimistic locking here ?

my guess,here we need to write pure C code.

Sep 29 '06 #1
6 4084
if by optimistic locking you mean "an interlock that will work 99.999%
of the time", you could try something like:

int Busy = 0;
while( Busy ) sleepsometime() ;
Busy++;
while( Busy 1 ) { Busy--; sleepsometime() ; Busy++; }
i = i * i - (i-j) * whatever;
Busy--;

Ad-hoc locks like this will work a lot of the time, just don't use them
for anything critical!

i.e. the value in "i" might sometimes not get updated properly if two
threads happened to barge thru the lock once in a while.

Sep 29 '06 #2

Ancient_Hacker wrote:
if by optimistic locking you mean "an interlock that will work 99.999%
of the time", you could try something like:
I meant http://en.wikipedia.org/wiki/Optimistic_locking
int Busy = 0;
while( Busy ) sleepsometime() ;
Busy++;
while( Busy 1 ) { Busy--; sleepsometime() ; Busy++; }
i can see race condition here .
i = i * i - (i-j) * whatever;
Busy--;

Ad-hoc locks like this will work a lot of the time, just don't use them
for anything critical!

i.e. the value in "i" might sometimes not get updated properly if two
threads happened to barge thru the lock once in a while.
Oct 1 '06 #3
shaanxxx wrote:
>Busy++;
while( Busy 1 ) { Busy--; sleepsometime() ; Busy++; }
i can see race condition here .
Then you have x-ray vision. Head to comp.programmin g.threads as you're
inferring threading and/or async signals.
Oct 1 '06 #4

Christopher Layne wrote:
shaanxxx wrote:
Busy++;
while( Busy 1 ) { Busy--; sleepsometime() ; Busy++; }
i can see race condition here .

Then you have x-ray vision.
was I wrong? How that x-ray vision is connected to programming.

Head to comp.programmin g.threads as you're
inferring threading and/or async signals.

Thanks

Oct 2 '06 #5

shaanxxx wrote:
I have global variable which is being shared between threads
Outside the scope of C programming per se.
(problem is not connected with thread).
Nonsense - the problem is intimately connected _at least_ with the fact
that multiple threads are acting on the variable. If you had no
threads, you'd have no concurrency problem, unless you moved to
multiple processes and shared memory (all outside the scope of C
programming per se).
Without using any mutex i have do some
operation global variable in *consistent* way.
Atomic operations (compare_and_sw ap etc) may be appropriate, but will
be platform-specific at least in their implementation details.
How can we implement the concept of optimistic locking here. I got lot
defination of optimistic locking on internet .

But i couldnt find any implementation. So i thought of implementing one
small programme . Can we apply concept of optimistic locking here ?
More research, but I'm uncertain that this gets you anywhere without
some form of atomic operation.
my guess,here we need to write pure C code.
Highly unlikely - the issues (multiple "tasks" sharing access to data)
are outside what the C language itself was developed for. To do this at
all robustly, you'll need to use things beyond the C language.

Oct 2 '06 #6

ma**********@po box.com wrote:
shaanxxx wrote:
I have global variable which is being shared between threads

Outside the scope of C programming per se.
(problem is not connected with thread).

Nonsense - the problem is intimately connected _at least_ with the fact
that multiple threads are acting on the variable. If you had no
threads, you'd have no concurrency problem, unless you moved to
multiple processes and shared memory (all outside the scope of C
programming per se).
I put above sentence so that this question will not removed from C
programming group.
Our approach for everything is pessimistic (called locking, LOCK
everything).

We have been using for long time so we always follow that.
Without using any mutex i have do some
operation global variable in *consistent* way.

Atomic operations (compare_and_sw ap etc) may be appropriate, but will
be platform-specific at least in their implementation details.
How can we implement the concept of optimistic locking here. I got lot
defination of optimistic locking on internet .

But i couldnt find any implementation. So i thought of implementing one
small programme . Can we apply concept of optimistic locking here ?

More research, but I'm uncertain that this gets you anywhere without
some form of atomic operation.
my guess,here we need to write pure C code.

Highly unlikely - the issues (multiple "tasks" sharing access to data)
are outside what the C language itself was developed for. To do this at
all robustly, you'll need to use things beyond the C language.
you can refer book on transaction processing by gim grey. it has
something for atomic operation "within c language". Still, I am not
sure what he has said in his books will work.

Oct 3 '06 #7

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

Similar topics

16
8905
by: Nid | last post by:
How do I do row-level locking on SQL Server? Thanks, Nid
1
3174
by: Chris Huddle | last post by:
Is there a way to turn off optimistic concurrency in ASP.NET when updating a record via the oleDBdataAdapter/oleDBcommandBuilder? If you use the Microsoft ASP.NET tools you can turn it off and thus modify the SQL statement so that it doesn't include the WHERE portion, but I'm not sure how to do the same thing in the code. I do my...
8
5020
by: Mike Kelly | last post by:
I've chosen to implement the "optimistic concurrency" model in my application. To assist in that, I've added a ROWVERSION (TIMESTAMP) column to my main tables. I read the value of the column in my select, remember it, and then use it in the update. It works just fine when I have full control of the whole process. I want to do the same for...
1
6288
by: jimi_usenet | last post by:
Hi, Can anyone tell me if MySQL uses optimistic or pessimistic locking with InnoDB tables? And can this be modified in any way? When I use the search function on the mysql site, it only gives me search results for MaxDB... Regards /Jimi
2
2186
by: John | last post by:
In 'Data Adapter Configuration Wizard' for OleDbDataAdapter, there's a checkbox called 'Use optimistic concurrency' that allows to turn on/off the option. I don't use the wizard, I create MyDataAdapter using the following, MyDataAdapter = new New OleDbDataAdapter(MySql, MyDatabase) The question is how to turn off the 'Use optimistic...
0
1555
by: russganz | last post by:
It seems to me there are real problems with the datagridview (along with the detailsview and form view) control when using optimistic concurrency with fields that can be null. Trying to use these controls I have many times had the update not work with no error generated. After much experimentation I have narrowed it down to how the Update...
1
1187
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I was suddenly told to whip up a web project in ASP.Net 2.0. The last few weeks have been a crash course in new technology. I've done some 1.1 web work but it's been a while. The basics are really quite simple and I want to keep it that way. I'm using virtually nothing but grid views and detailsviews. The grids are used solely for display....
1
2044
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
(If I'm overlooking anything, please let me know.) First, my only concern is updating single records in a Detailsview using an ObjectDataSource. The target table has a timestamp field. Assume a single primary key. Create your xsd. Drag the table onto the xsd. Then manually edit the Update statement to simplify it. Essentially...
8
3965
by: Roger.Noreply | last post by:
Hi, Sql-Server 2000, 2005. A report fetches a lot of rows using the "WITH (ROWLOCK)" syntax (the sql is generated on the fly by a tool and not easily changeable). SELECT col1, col2 FROM mytab WITH (ROWLOCK) WHERE ...." The select-clause runs for several minutes. Another user fetches one of those rows and tries to update it. The result...
0
7614
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. ...
0
7777
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...
0
7713
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...
0
5916
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5301
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...
0
3414
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1844
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
990
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
671
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.