开发者

java, c#, vb: Can we write thread safety functions without relying on native implementations

I'm wondering is it ever possible to write locking-features(for threading) with the language itself (this question is purely academic, I know no one will ever do it even if its possible (not me))?

Rephrasing the question:

A) In Java, is it possible to write thread safety functions with java alone, but without using any of the provided classes/language elements/syntax which offer this feature?

B) In C#, is it possible to write thread safety functions with C# alone, but without using any of the provided classes/language elements/syntax which offer this feature?

C) In Vb, is it possible to write thread safety functions with Vb alone, but without using any of the provided classes/language开发者_Python百科 elements/syntax which offer this feature?


If you have atomic reads and writes, which C# does for ints for example, you can use Dekker's algorithm to build thread-safe algorithms without support from any other primitives:

  • Dekker's algorithm


Yes it is possible, all one really needs to form a mutex is a shared memory variable that is written in a single read-modify-write machine cycle. However, it is inefficient because all the threads in potential conflict have to busy-wait. A major point of the more advanced constructs in the thread contention avoidance classes in these languages is to allow a thread to quit executing until another thread is done with the conflicted resource and then be immediately put back into the execution train when the resource frees up.

I think maybe you are thinking about:

C# lock()
VB SyncLock
Java syncronized

Those are language elements, not library classes.


No. If you have a thread safe program, you must be using some of the provided classes/language elements/syntax which offer thread safety.

The semantics of volatile in Java is defined in Java Language Spec Section 17, "Threads and Locks"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜