多线程编程指南

Solaris 线程的独有函数

本节介绍 Solaris 线程的独有函数:用于暂停执行线程和继续执行暂停的线程。

暂停执行线程

thr_suspend(3C) 可用来立即暂停执行 target_thread 所指定的线程。如果从 thr_suspend() 成功返回,则将不再执行暂停的线程。

因为 thr_suspend() 在暂停目标线程时不会考虑该线程可能持有的锁,所以,在使用 thr_suspend() 时一定要格外小心。如果要暂停的线程调用的函数需要由已暂停的目标线程拥有的锁,则将产生死锁。

thr_suspend 语法

#include <thread.h>



int thr_suspend(thread_t tid);

线程暂停之后,以后调用 thr_suspend() 将不起任何作用。信号无法唤醒暂停的线程。线程恢复执行之前,信号将一直保持暂挂状态。

在以下概要中,pthread 中定义的 pthread_t tid 与 Solaris 线程中定义的 thread_t tid 相同。这两个 tid 值可以通过赋值或通过使用强制转换来互换使用。

thread_t tid; /* tid from thr_create() */



/* pthreads equivalent of Solaris tid from thread created */

/* with pthread_create() */

pthread_t ptid;	



int ret;



ret = thr_suspend(tid);



/* using pthreads ID variable with a cast */

ret = thr_suspend((thread_t) ptid);	

thr_suspend 返回值

thr_suspend() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下情况,thr_suspend() 将失败并返回对应的值。


ESRCH

描述:

当前的进程中找不到 tid

继续执行暂停的线程

thr_continue(3C) 可用来恢复执行暂停的线程。继续执行暂停的线程之后,以后调用 thr_continue() 将不起任何作用。

thr_continue 语法

#include <thread.h>



int thr_continue(thread_t tid);

信号无法唤醒暂停的线程。thr_continue() 继续执行暂停的线程之前,信号将一直保持暂挂状态。

在 pthread 中定义的 pthread_t tid 与在 Solaris 线程中定义的 thread_t tid 相同。这两个 tid 值可以通过赋值或通过使用强制转换来互换使用。

thread_t tid; /* tid from thr_create()*/



/* pthreads equivalent of Solaris tid from thread created */

/* with pthread_create()*/

pthread_t ptid;	



int ret;



ret = thr_continue(tid);



/* using pthreads ID variable with a cast */

ret = thr_continue((thread_t) ptid)	

thr_continue 返回值

thr_continue() 在成功完成之后返回零。其他任何返回值都表示出现了错误。如果出现以下情况,thr_continue() 将失败并返回对应的值。


ESRCH

描述:

当前的进程中找不到 tid