views:

121

answers:

1

I want my function to only run once. Meaning that if many threads call it at the same time, the function will block all threads apart from one and only allow it to run.

A: 

It sounds like you want the stored procedure to do the synchronization. Why not just put the synchronization in the application itself.

pthread_mutex_lock(&lock);
... Call stored procedure here ..
pthread_mutex_unlock(&lock);

If you need to provide synchronization at the database level, you can use the LOCK and UNLOCK TABLES commands within your stored procedure. Lock on entry to the procedure and unlock on exit. Depending on your needs, it may be that you'll want to create a dummy table that is locked and unlocked instead of an actual data table in use by this and other processes. See here for more details.

RC
I have many processes calling this function. I don't want it synchronized with respect to threads but with respect to processes.
Guy