手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网站运营>建站经验>列表

POSIX Thread Tutorial

来源:互联网 作者:west263.com 时间:2008-04-16
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Sometimes you do not care about the thread's exit status; in this case, having to call pthread_join() becomes an annoyance. The pthread_detach() function is helpful in such cases. You call pthread_detach() with a pthread_t argument. This argument must be the descriptor for a running thread. After the call completes, it is no longer possible for the rest of the program to reference that thread in any way. The selected thread simply runs to completion, at which point its systems resources are automatically reclaimed. This might be a useful way to implement runtime graphics: you have one of these "free" threads send your data to some graphics program.

The source for simple.c looks a little nastier than it needs to. For example, much of the code is related to error checking of the pthread_xxx calls. To help eliminate some of this clutter, I have written a small library of macros and C code to hide some of these details. The macros and library calls all have the prefix "pt_" to distinguish them from the pthreads calls they wrap.

They all have the form:

                pt_xxx(object *obj,..., char *msg)
The "object *" is always a pointer to a pthreads object (or one of the derived types discussed later). The "char *msg" is a 
message that is printed in case of an error. When errors occur, your code will exit. Since most pthreads routines only return 
the EINVAL error, this seems acceptable -- usually it means that you've attempted to operate on an invalid pointer. Let's use
 these macros to clean things up a bit -- the new code is called simple2.c (click here for simple2.c; click here for pt.h ): 
 1: /******************************************************************
 2:  * simple2.c -- multithreaded "hello world" using pt_xxx macros
 3:  *
 4:  * Author: Mark Hays 
 5:  */
 6: 
 7: #include "pt.h"
 8: 
 9: #include 
10: 
11: #define NTHREADS 4
12: 
13: /******** this is the thread code */
14: pt_addr_t hola(pt_addr_t arg)


文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!