Multi threading

Green Tea support multi threading processing. Includes:

  • File-based multi-threading (simple)
  • Function-based multi-threading,

Vars will not be shared among threads, you must use parameters and return values to exchange data among threads.

File based multi threading

Use @create_file_thread function to create a thread that execute another file. On thread file, you could return value to main thread using thread_return. Don’t use exit in thread file, it will also exit main thread.

File thread1.gtc :

for 3 times

@sleep 1 // wait 1s

“Thread1 – running for “+ $_time + “s”

thread_return $_args[0] // file argument #1

File main.glc:

$thread1 : (@create_file_thread “thread1.gtc Hello”)

“Start thread1”

@thread_start $thread1

@thread_wait $thread1

$result : (@thread_result $thread1)

“Thread1 result is: ” + $result

Run main.glc

> Start thread1

> Thread1 – running for 1s

> Thread1 – running for 2s

> Thread1 – running for 3s

> Thread1 result is: Hello

File based multi threading should have parameters which are numbers or strings, like “Hello” in the above example.

Function based multi-threading

Use @create_function_thread function to create a thread that execute a function. On thread file, you could return value to main thread using normal return. Don’t use exit in thread function, it will also exit main thread.

@thread1 $param

for 3 times

@sleep 1 // wait 1s

“Thread1 – running for “+ $_time + “s”

return $param // file argument #1

$thread1_function : @thread1

$thread1 :(@create_file_thread $thread1_function, “Hello”)

“Start thread1”

@thread_start $thread1

@thread_wait $thread1

$result : (@thread_result $thread1)

“Thread1 result is: ” + $result

> Start thread1

> Thread1 – running for 1s

> Thread1 – running for 2s

> Thread1 – running for 3s

> Thread1 result is: Hello

Function based multi threading could have any kind of parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *