Welcome Guest ( Log In | Register )

Outline · [ Standard ] · Linear+

 alternative to "lock" ?, c#

views
     
TSnarf03
post Mar 25 2024, 11:40 PM, updated 2y ago

Look at all my stars!!
*******
Senior Member
4,547 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


in c#, "lock" is a method to avoid racing condition, avoid multiple threads read and write to a shared variable between them.

lock usually will cause delay, lets say 1 thread locked and accessing a variable(and process until it unlock), all others thread cant lock, they all have to wait(or stuck) until somebody else lock, unlock, and wait until they able to lock(access the variable)

i wonder if there is alternative, like i want to know if the variable is locked, if its, pass, try it again next time, still lock, pass, basically i dont want certain thread get delay when they cant access a variable.
Lord Tiki Mick
post Apr 8 2024, 04:26 AM

Regular
******
Senior Member
1,020 posts

Joined: Jul 2012
QUOTE(narf03 @ Mar 25 2024, 11:40 PM)
in c#, "lock" is a method to avoid racing condition, avoid multiple threads read and write to a shared variable between them.

lock usually will cause delay, lets say 1 thread locked and accessing a variable(and process until it unlock), all others thread cant lock, they all have to wait(or stuck) until somebody else lock, unlock, and wait until they able to lock(access the variable)

i wonder if there is alternative, like i want to know if the variable is locked, if its, pass, try it again next time, still lock, pass, basically i dont want certain thread get delay when they cant access a variable.
*
Look for lock with timeout solution for C#.
TSnarf03
post Apr 8 2024, 04:36 AM

Look at all my stars!!
*******
Senior Member
4,547 posts

Joined: Dec 2004
From: Metro Prima, Kuala Lumpur, Malaysia, Earth, Sol


QUOTE(Lord Tiki Mick @ Apr 8 2024, 04:26 AM)
Look for lock with timeout solution for C#.
*
cant wait, want to do as fast as possible, but i just noticed that "byte" operation is atomic, means if you just use 1 byte as variable, multiple threads can read and write on that byte, you dont need to worry about lock or race condition. so 1 byte is limited to 256 possibilities.
dstl1128
post May 24 2024, 12:04 PM

Look at all my stars!!
*******
Senior Member
4,464 posts

Joined: Jan 2003
atomic increment & decrement, and checking are another "syncing" operation. But yeah, what you need seems like a timeout.


[update] Interlocked.CompareExchange , Interlocked.Increment .. etc... search under this namespace.

This post has been edited by dstl1128: May 24 2024, 12:07 PM
khew
post Aug 17 2024, 10:13 AM

New Member
*
Junior Member
24 posts

Joined: Feb 2013


In C, you can use atomic int as a flag, which does not involve sync at all since it is just a single assembly instruction. So if this flag is set, then your thread can do something else.
babisotong
post Dec 16 2024, 11:28 AM

On my way
****
Junior Member
654 posts

Joined: Feb 2013


QUOTE(narf03 @ Mar 25 2024, 11:40 PM)
in c#, "lock" is a method to avoid racing condition, avoid multiple threads read and write to a shared variable between them.

lock usually will cause delay, lets say 1 thread locked and accessing a variable(and process until it unlock), all others thread cant lock, they all have to wait(or stuck) until somebody else lock, unlock, and wait until they able to lock(access the variable)

i wonder if there is alternative, like i want to know if the variable is locked, if its, pass, try it again next time, still lock, pass, basically i dont want certain thread get delay when they cant access a variable.
*
so, you already got solution for your question?

mind sharing?

i did ask chatgpt is about the question

"If you want to avoid blocking threads when accessing a shared resource, Monitor.TryEnter, Interlocked.CompareExchange, and SemaphoreSlim are great alternatives to using the lock keyword. They allow threads to try and acquire a lock or access a resource without getting blocked. Each approach has its strengths, and the best choice depends on your specific use case, whether you need to ensure atomicity of operations, or if you are working in an asynchronous environment."
SUSputeraracing01
post Dec 26 2024, 04:33 AM

New Member
*
Newbie
14 posts

Joined: Jul 2016


Yeah, I’ve run into this issue before when I wanted to avoid threads getting stuck waiting for a lock. Instead of using lock, you can try Monitor.TryEnter(). It lets you check if the variable is locked, and if it is, the thread just skips instead of waiting around. I used this in a project where performance mattered more than ensuring every thread got immediate access to the resource. Here's the gist: you wrap your code in a try block when TryEnter succeeds, and make sure to call Monitor.Exit() in the finally. It worked for me, but you’ve gotta be okay with some threads not always getting the resource on every pass.








 

Change to:
| Lo-Fi Version
0.0151sec    1.37    5 queries    GZIP Disabled
Time is now: 23rd December 2025 - 03:57 AM