"Sputnik" help  
Sputnik Help
lock(<name/UniqueId>)
{
statements
...
} lock()~;

Description

This allows creating a thread-lock on a section of code so only one thread may execute it at a time.

Parameters

name/UniqueId

A string containing a unique name/Id to define this lock as.
Note - Multiple locks may use the same uniqueId and they will cause a lock to all the others when triggered to any.

Remarks

The lock statement in Sputnik provides a way to create a thread-lock on a specific section of code, ensuring that only one thread can execute it at a time. By using locks, you can prevent concurrency issues and ensure data integrity when multiple threads are accessing shared resources.

To create a lock, you need to provide a unique name or unique identifier for the lock. This identifier helps differentiate between different locks, and multiple locks can use the same identifier, causing them to synchronize with each other.

When a thread encounters a lock statement, it checks if the lock is currently held by another thread. If it is, the thread waits until the lock is released by the other thread before proceeding. This ensures that only one thread executes the locked code section at any given time.

The lock statement follows the synchronized function rule, which means that only one thread can execute the locked section of code, and other threads are blocked until the lock is released.

Using locks can help prevent race conditions and maintain data consistency in multi-threaded environments. It ensures that critical sections of code are executed atomically, without interference from other threads.

In the provided example, a thread is created to run the Test function. Inside the Test function, a lock is created with the name "testy." The locked code section is executed, and other threads attempting to access the same lock are blocked until the lock is released. This guarantees that only one thread runs the code inside the lock at a time.

By using locks, you can create a safe and controlled environment for multi-threaded programming, ensuring that shared resources are accessed correctly and preventing conflicts between threads.

Related

Rule Synchronized, Synchronized Function Rule

Example

Example:

// Create a thread that runs on the function
ThreadCreate("testThread", &Test);
sleep(1000); // give the thread enough time to reach the function

Function Test()
{
    lock ("testy") // create the lock
    {
        say "Test function running";
        sleep(5000); // Sleep for 5 seconds
        say "Test function ended";
    }
}

say "Trying to run Test function...";
// after the above text there will be a 4 second pause
// since the function is waiting for the other thread to
// end before allowing our thread to call it
Test();

// PRINTS
// Test function running
// Trying to run Test function...
// Test function ended
// Test function running
// Test function ended

As you can see each thread could only run the code inside the lock if the other was not running it.

This is a safe way of making sure only one thread runs the section of code at a time.

Same as above but this using the deferred lock meaning it becomes active until the function ends without needing { } blocks:

// Create a thread that runs on the function
ThreadCreate("testThread", &Test);
sleep(1000); // give the thread enough time to reach the function

Function Test()
{
    lock ("testy")~; // remains in effect until end of the function
    say "Test function running";
    sleep(5000); // Sleep for 5 seconds
    say "Test function ended";
}

say "Trying to run Test function...";
// after the above text there will be a 4 second pause
// since the function is waiting for the other thread to
// end before allowing our thread to call it
Test();

// PRINTS
// Test function running
// Trying to run Test function...
// Test function ended
// Test function running
// Test function ended

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org