Core Function ThreadCreate

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> ThreadCreate( <name>, <function> ) </pre> === Description === Create a new thread and start it off executing a function or some code Sputnik supports multi-threading and...")
 
m (1 revision)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
ThreadCreate( <name>, <function> )
+
ThreadCreate( <name>, <function>, <extra> )
 
</pre>
 
</pre>
  
Line 8: Line 8:
  
 
Sputnik supports multi-threading and you can create threads in your code to assist you.
 
Sputnik supports multi-threading and you can create threads in your code to assist you.
 +
 +
Threads work best if you use them as *worker threads*.
  
 
=== Parameters ===
 
=== Parameters ===
Line 17: Line 19:
 
==== function ====
 
==== function ====
  
Either a function to call or a string to exeute similar to Eval()
+
Either a function to call or a string to execute similar to Eval()
 +
 
 +
 
 +
==== extra====
 +
 
 +
Optional; A variable to passed to the thread as $arg for use
  
 
=== Return Value ===
 
=== Return Value ===
Line 31: Line 38:
 
* Threads can cause problems and even crash if used improperly.
 
* Threads can cause problems and even crash if used improperly.
  
* A thread works best if its like a worker thread that is designed to get/set global variables and do its own thing and call functions here and there, Threads dont like to be messing with every peice of code that you are messing with.
+
* A thread works best if it's like a worker thread that is designed to get/set global variables and do its own thing and call functions here and there. Threads don't like to mess with every piece of code that you are messing with.
  
* Threads have their own stack and local variable that is independant from everything else they can however access global variables and functions etc.
+
* Threads have their own stack and local variable that is independent from everything else they can however access global variables and functions etc.
  
* Threads dont like GUI stuff since most GUI functions are to be edited by the thread that created them therefor other threads arent too happy if you make them mess with GUI of the main thread etc.
+
* Threads don't like GUI stuff since most GUI functions are to be edited by the thread that created them therefore other threads aren't too happy if you make them mess with GUI of the main thread etc.
  
 
=== Example ===
 
=== Example ===
Line 62: Line 69:
 
sleep(500);
 
sleep(500);
 
}
 
}
 +
}
 +
</syntaxhighlight>
 +
 +
Here the extra param is used so it stores a variable in this case it stores the class object
 +
 +
<syntaxhighlight lang="sputnik">
 +
Class Testy
 +
{
 +
my $Name;
 +
Function __Construct( $Name )
 +
{
 +
$this->$Name = $Name;
 +
ThreadCreate("th1$Name", '$arg->ThreadFunc();', $this);
 +
}
 +
Function ThreadFunc()
 +
{
 +
while(true)
 +
{
 +
Println("$this->$Name | Hello from thread: '". ThreadName() . "'");
 +
sleep(1000);
 +
}
 +
}
 +
};
 +
 +
$Testy1 = new Testy("Meow");
 +
$Testy2 = new Testy("lol");
 +
 +
while(true)
 +
{
 +
DoEvents();
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

ThreadCreate( <name>, <function>, <extra> )

Contents

Description

Create a new thread and start it off executing a function or some code

Sputnik supports multi-threading and you can create threads in your code to assist you.

Threads work best if you use them as *worker threads*.

Parameters

name

Name of a thread to create.

function

Either a function to call or a string to execute similar to Eval()


extra

Optional; A variable to passed to the thread as $arg for use

Return Value

Success: Returns 1.

Failure: Returns 0.

Remarks

You can spawn as many threads as you wish however there are a few important things to consider with threads.

Example

ThreadCreate("th1", "Thread1Func();"); // This thread will execute a function
ThreadCreate("th2", "Thread2Func();"); // This thread will execute a function
ThreadCreate("th3", 'println("Thread 3 says hi");'); // This thread will execute code
 
inputc();
 
Function Thread1Func()
{
	while(true)
	{
		println("Hello from thread '" . ThreadName() . "'");
		sleep(500);
	}
}
 
Function Thread2Func()
{
	while(true)
	{
		println("Heya from thread '" . ThreadName() . "'");
		sleep(500);
	}
}

Here the extra param is used so it stores a variable in this case it stores the class object

Class Testy
{
	my $Name;
	Function __Construct( $Name )
	{
		$this->$Name = $Name;
		ThreadCreate("th1$Name", '$arg->ThreadFunc();', $this);
	}
	Function ThreadFunc()
	{
		while(true)
		{
			Println("$this->$Name | Hello from thread: '". ThreadName() . "'");
			sleep(1000);
		}
	}
};
 
$Testy1 = new Testy("Meow");
$Testy2 = new Testy("lol");
 
while(true)
{
	DoEvents();
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox