Lock
This function locks a mutex. When a mutex is locked, other threads that lock the mutex will pause until the locking thread calls Mutex::Unlock().
Syntax
Example
#include "Leadwerks.h"
using namespace Leadwerks;
Mutex* mutex = NULL;
Object* ThreadFunction(Object* object)
{
for (int i = 0; i<1000; i++)
{
//Lock the mutex so the two threads dont interfere with each other
mutex->Lock();
System::Print("Thread 2 printing...");
//Unlock the mutex so the other thread can run
mutex->Unlock();
Leadwerks::Time::Delay(1);
}
//Create something to give back to the main thread
Vec3* v = new Vec3(1, 2, 3);
return v;
}
int main(int argc, const char *argv[])
{
//Create a mutex
mutex = Mutex::Create();
//Create a thread. This will launch into the thread function
Thread* thread = Thread::Create(ThreadFunction);
//Wait for the thread to finish
while (thread->GetState() != Thread::Finished)
{
//Lock the mutex so the two threads don't interfere with each other
mutex->Lock();
System::Print("Thread 1 printing...");
//Unlock the mutex so the other thread can run
mutex->Unlock();
Leadwerks::Time::Delay(1);
}
//Get the thread result
Object* result = thread->GetResult();
System::Print(result);
//Cleanup
result->Release();
thread->Release();
mutex->Release();
return 0;
}