Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between TVar and TMVar

I've seen the TVar is a simple container, while the TMVar is the same as an MVar, meaning it has a lock etc, but within the STM monad. I am wondering why would that be necessary, as the idea of the STM is to make locks unnecessary.

So which is the one to use if you, say have a type like [Handle] a list of socket handles that you want to use between threads made by forkIO?

like image 630
Lanbo Avatar asked Aug 02 '11 16:08

Lanbo


People also ask

Is TMVR the same as TAVR?

Another distinction between TAVR and TMVR is the etiology and natural progression of the underlying disease and driving factors for intervention that are vastly different between aortic and mitral valve disease.

What is a TMVR procedure?

What Is TMVR? Transcatheter mitral valve replacement is a type of mitral valve replacement and repair surgery that allows cardiologists to treat severe mitral regurgitation or mitral stenosis without surgery.

Is TMVR the same as MitraClip?

A type of transcatheter mitral valve repair (TMVR), MitraClip is a relatively new heart valve procedure. We use this minimally invasive approach to treat the main form of mitral valve leaking, or mitral regurgitation. To perform TMVR, your doctor inserts a catheter, or a small, thin tube, into an artery in your groin.

What is the difference between TAVR and Tavi?

TAVI stands for transcatheter aortic valve implantation. The procedure and its approaches are the same as TAVR. Your doctor may use the terms interchangeably when discussing your treatment options.


1 Answers

It's not really a matter of locking, it's about what the reference means:

  • TVar is a mutable reference within STM, representing general shared state. You create it holding a value, you can read and write to it, etc. It's very much akin to IORef or STRef (which are the same thing anyway).

  • TMVar is a reference to a slot that threads can use to communicate. It can be created holding a value, or empty. You can put a value into it, which if already filled blocks until someone else empties it; or you can take a value from it, which if already empty blocks until someone fills it. It's obviously akin to an MVar, but for many common uses it might be simpler to think of it as a single-element queue used for a communicating producer/consumer pair.

In short, TVar is general shared state, use it if you want atomic updates to data from arbitrary places. TMVar is a synchronization primitive, use it if you want a thread to wait until something becomes available, while another waits for something to be needed.

Also note TChan, which is implemented roughly as two TVars holding locations in a linked list where each forward link is also a TVar, and works as an unbounded queue for communication.

All of these can be used in slightly different ways, of course--you can peek at the value of a TMVar without removing it, for instance, if you want a scenario where multiple threads all wait for a single resource to become available but it's never "used up".

like image 147
C. A. McCann Avatar answered Sep 23 '22 01:09

C. A. McCann