List:General Discussion« Previous MessageNext Message »
From:Michael T. Babcock Date:January 3 2003 5:38pm
Subject:Info: Using MySQL as Mutex
View as plain text  
Just as an FYI to fellow developpers out there (feeling generous today):

How many times have you written a semaphore locking system for your 
programs and worried they weren't thread-safe, or wished you didn't have 
to?  Well, if you're feeling especially lazy (as I often do) and you 
have MySQL on hand, try using its locking features instead of writing 
your own (especially if your program already involves MySQL).  Connect 
to MySQL [this assumes you're using InnoDB/BDB], do a BEGIN and try to 
UPDATE a row in a locks table for your mutex (UPDATE Locks SET Holder = 
"me" WHERE Name = "ThreadMutex4") and do your work.  MySQL will block 
the UPDATE until the previous thread (if any) rolls back or commits its 
transaction.

Any thoughts / technical comments? (I just started doing this in a large 
internal log tracking project that does, in fact, use MySQL) :-)

-- 
Michael T. Babcock
C.T.O., FibreSpeed Ltd.
http://www.fibrespeed.net/~mbabcock


Thread
Info: Using MySQL as MutexMichael T. Babcock3 Jan
RE: Info: Using MySQL as MutexDana Diederich3 Jan
  • Re: Info: Using MySQL as MutexJeremy Zawodny4 Jan