Thread

Multi-threading

Green Tea support multi threads processing. Includes:

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

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

Only main thread could spawn sub-threads. Thread cannot be initialized from a non main thread.

 

At the time the main thread exit , it will wait if there is any running sub-threads with a warning message. Press Control +C to exit.

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 1000 // wait 1s

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

thread_return (@thread_get_args)[0] // file argument #1

File main.glc:

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

@echo “Start thread1”

@thread_get_result $thread1

$result : (@thread_result $thread1)

@sleep 1000 // sleep for 1s

“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

(under development)

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.