.. index:: 
	single: Threads; はじめに

================
Threads 拡張機能
================

この章では Threads 拡張機能の用法を解説します。

.. index:: 
	pair: Threads; スレッドの作成

スレッドの作成
==============

用例 (1):

.. code-block:: ring

	load "threads.ring"

	func main

		t = new_thrd_t()
		thrd_create(t,"Hello()")
		res = 0
		thrd_join(t, :res)

	func Hello

		? "Hello() 関数からのメッセージ"

.. index:: 
	pair: Threads; 追加の用例

追加の用例
==========

用例 (2):

.. code-block:: ring

	load "threads.ring"

	func main

		t = new_thrd_t()
		thrd_create(t,"Hello()")
		res = 0
		thrd_join(t, :res)

		t2 = new_thrd_t()
		thrd_create(t2,"Hello2()")
		res2 = 0
		thrd_join(t2, :res2)

		? :done

	func Hello

		? "the Hello() 関数からのメッセージ"

	func Hello2

		? "the Hello2() 関数からのメッセージ"

用例 (3):

.. code-block:: ring

	load "threads.ring"

	func main

		nThreads = 2
		aList = list(nThreads)

		for x=1 to nThreads
			aList[x] = new_thrd_t()
			thrd_create(aList[x],"Hello("+x+")")
		next

		for x=1 to nThreads
			res= 0
			thrd_join(aList[x],:res)
		next

		? :Done

		shutdown()

	func Hello x

		for r=1 to 100
			? "("+r+") Hello("+x+") 関数からのメッセージ"
		next

用例 (4):

.. code-block:: ring

	load "threads.ring"

	func main

		nThreads = 10
		aList = list(nThreads)

		for x=1 to nThreads
			aList[x] = new_thrd_t()
			thrd_create(aList[x],"Hello("+x+")")
		next

		for k=1 to nThreads
			 ? "*** 参加前: " + k + " *** "
			 res = 0
			 thrd_join(aList[K],:res)
			 ? "*** 参加後: " + k + " *** "
		next

		? " ===== 完了 ========== "

	func Hello x

		for r=1 to 100
			? "("+r+") Hello("+x+") 関数からのメッセージ"
		next

.. index:: 
	pair: Threads; リファレンス

リファレンス
============

定数:

* TIME_UTC
* TINYCTHREAD_VERSION_MAJOR
* TINYCTHREAD_VERSION_MINOR
* TINYCTHREAD_VERSION
* thrd_error
* thrd_success
* thrd_timedout
* thrd_busy
* thrd_nomem
* mtx_plain
* mtx_timed
* mtx_recursive

関数:

* int mtx_init(mtx_t \*mtx, int type)
* void mtx_destroy(mtx_t \*mtx)
* int mtx_lock(mtx_t \*mtx)
* int mtx_timedlock(mtx_t \*mtx, const struct timespec \*ts)
* int mtx_trylock(mtx_t \*mtx)
* int mtx_unlock(mtx_t \*mtx)
* int cnd_init(cnd_t \*cond)
* void cnd_destroy(cnd_t \*cond)
* int cnd_signal(cnd_t \*cond)
* int cnd_broadcast(cnd_t \*cond)
* int cnd_wait(cnd_t \*cond, mtx_t \*mtx)
* int cnd_timedwait(cnd_t \*cond, mtx_t \*mtx, const struct timespec \*ts)
* int thrd_create(thrd_t \*thr, const char \*cEvent)
* thrd_t thrd_current(void)
* int thrd_detach(thrd_t thr)
* int thrd_equal(thrd_t thr0, thrd_t thr1)
* void thrd_exit(int res)
* int thrd_join(thrd_t thr, int \*res)
* int thrd_sleep(const struct timespec \*duration, struct timespec \*remaining)
* void thrd_yield(void)
* void tss_delete(tss_t key)
* void \*tss_get(tss_t key)
* int tss_set(tss_t key, void \*val)
