Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 操作系统 > Linux > Posix Thread 多线程 Pthreads
【标  题】:Posix Thread 多线程 Pthreads
【关键字】:Posix,Thread,Pthreads
【来  源】:http://www.cublog.cn/u/17564/showart.php?id=103786

Posix Thread 多线程 Pthreads

Your Ad Here     这取决于exec的implementation。main函数的执行,是在一个主线程里面,而创建,管理其他thread,可以参照以下列举的一些函数:
1.int pthread_create(pthread_t * threadid,const pthread_attr_t * attr,void * (*func)(void *),void * arg);
    OR:
  typedef void * (func)(void *);
  int pthread_create(pthread_t * threadid,const pthread_attr_t * attr,func * fuction,void * arg);
   
    哈哈,可能刚刚开始看的时候有一点点迷惑。仔细想想就可以了。

  NOTE:The last two parameters have the type of "void *",this means that we can pass any data we want to the thread function,meanwhile,we can return any data we want to the calling process.

2.pthread_t pthread_self(void);

    一般用来在线程内部调用,以在线程内部获得本线程的threadid。
  NOTE:试试看,在main函数里面调用pthread_self(),看看结果。

3.int pthread_exit(void * thread_return);
    在线程内部,使用pthread_exit((void *)0);可以返回线程的退出状态给调用pthread_join(thread_t threadid,void ** status)的function。

4.int pthread_join(pthread_t threadid,void ** status);
    调用此函数的thread将block,直到threadid所代表的那个thread调用pthread_exit(void *).
    如果调用时将status设置为NULL,则所等待的那个thread的退出状态被丢弃。

5,int pthread_detach(pthread_t threadid);
    如果要将join之后的thread变成非join的,就调用这个。非join的thread终止之后,所有的资源都释放了。即不保留退出状态。
NOTE:如果不显式detach,那么就是join的。状态保留到直到调用了对这个线程的pthread_join为止。

6.int pthread_cancle(pthread_t threadid);
    调用此函数将导致threadid所指定的那个线程退出,行为相当于threadid所指定的那个线程调用pthread_exit(PTHREAD_CANCELED).哈哈。

基本上这几个就可以用来编制并行程序了。但是并行程序中的同步,嘿嘿,以后有空再说了。
   
用GRUB(包括WINGRUB)命令行模式引导安装Linux:【上一篇】
在Linux下设置Apache虚拟主机:【下一篇】
【相关文章】
  • ThreadLocal的设计与使用
  • 根据窗体句柄得到ThreadID
  • IPC学习(7)--Posix 消息队列
  • IPC学习(4)--posix semaphore
  • tomcat 并发问题 (terminating thread)
  • 多线程(6): Java Threads例子的一个问题
  • (汇)Delphi+MultiThread
  • Posix线程编程指南(1)线程创建与取消
  • 利用ThreadLocal记录日志
  • boost::thread 中使用类成员作为线程入口
  • 【随机文章】
  • Deadlock or hang
  • 最简单的iBatis入门例子
  • SMI-S期待更多
  • 传奇世界 【1.72版便民工程】便民调查
  • JSP用连接池连数据库的问题
  • Java3D实现三维显示 系列一
  • 整理Web开发-Ajax社区中有关Ajax的资料
  • solaris基础和常用知识
  • 系统管理员工具包: 备份关键的信息
  • 在ASP.NET Atlas中创建自定义的Transformer
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.