473,394 Members | 1,810 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,394 software developers and data experts.

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 4067
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.programming.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.programming.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_swap 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**********@pobox.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_swap 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
by: Nid | last post by:
How do I do row-level locking on SQL Server? Thanks, Nid
1
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...
8
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...
1
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...
2
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...
0
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...
1
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...
1
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 ...
8
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...

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.