易截截图软件、单文件、免安装、纯绿色、仅160KB

linux 线程编程

本章介绍 POSIX 线程的基本线程编程例程。本章介绍缺省线程(即,具有缺省属性值的线程),这是多线程编程中最常用的线程。本章还介绍如何创建和使用具有非缺省属性的线程。
本章介绍的 POSIX 例程具有与最初的 Solaris 多线程库相似的编程接口。
线程库
下面简要论述了特定任务及其相关手册页。
创建缺省线程
如果未指定属性对象,则该对象为 NULL,系统会创建具有以下属性的缺省线程:
进程范围
非分离
缺省栈和缺省栈大小
零优先级
还可以用 pthread_attr_init() 创建缺省属性对象,然后使用该属性对象来创建缺省线程。有关详细信息,请参见初始化属性一节。
pthread_create 语法
使用 pthread_create(3C) 可以向当前进程中添加新的受控线程。
int pthread_create(pthread_t *tid, const pthread_attr_t *tattr,
void*(*start_routine)(void *), void *arg);
#include <pthread.h>
pthread_attr_t() tattr;
pthread_t tid;
extern void *start_routine(void *arg);
void *arg;
int ret;
/* default behavior*/
ret = pthread_create(&tid, NULL, start_routine, arg);
/* initialized with default attributes */
ret = pthread_attr_init(&tattr);
/* default behavior specified*/
ret = pthread_create(&tid, &tattr, start_routine, arg);
使用具有必要状态行为的 attr 调用 pthread_create() 函数。 start_routine 是新线程最先执行的函数。当 start_routine 返回时,该线程将退出,其退出状态设置为由 start_routine 返回的值。请参见pthread_create 语法。
当 pthread_create() 成功时,所创建线程的 ID 被存储在由 tid 指向的位置中。
使用 NULL 属性参数或缺省属性调用 pthread_create() 时,pthread_create() 会创建一个缺省线程。在对 tattr 进行初始化之后,该线程将获得缺省行为。
pthread_create 返回值
pthread_create() 在调用成功完成之后返回零。其他任何返回值都表示出现了错误。如果检测到以下任一情况,pthread_create() 将失败并返回相应的值。
EAGAIN
描述:
超出了系统限制,如创建的线程太多。
EINVAL
描述:
tattr 的值无效。
等待线程终止
pthread_join() 函数会一直阻塞调用线程,直到指定的线程终止。
pthread_join 语法
使用 pthread_join(3C) 等待线程终止。
int pthread_join(thread_t tid, void **status);
#include <pthread


相关文档:

将Linux代码移植到Windows的简单方法

一.前言
  Linux拥有丰富各种源代码资源,但是大部分代码在Windows平台情况是无法正常编译的。Windows平台根本无法直接利用这些源代码资源。如果想要使用完整的代码,就要做移植工作。因为C/C++ Library的不同和其他的一些原因,移植C/C++代码是一项困难的工作。本文将以一个实际的例子(Tar)来说明如何把Linux代码移植 ......

linux启动时挂载rootfs的几种方式

[转]linux启动时挂载rootfs的几种方式
一直对linux启动时挂载根文件系统的过程存在着很多疑问,今天在水木精华区找到了有用的资料,摘录如下:
1。linux启动时,经过一系列初始化之后,需要mount 根文件系统,为最后运行init进程等做准备,mount
根文件系统有这么几种方式:
1)文件系统已经存在于硬盘(或者类似的设备 ......

[转]深入Linux网络核心堆栈

创建时间:2003-08-22
文章提交:raodan (raod_at_30san.com)
==Phrack Inc.==



              卷标 0x0b, 期刊号 0x3d, Phile #0x0d of 0x0f



|=---------------------=[ 深入Linux网络核心堆栈 ]=-----------------------= ......

linux 时间&定时器 介绍

1.时间表示
  在程序当中,我们经常要输出系统当前的时间,比如我们使用date命令的输出结果.这个时候我们可以使用下面两个函数:
  #include
  time_t time(time_t *tloc);
  char *ctime(const time_t *clock);
  time函数返回从1970年1月1日0点以来的秒数.存储在time_t结构之中.不过这个函数的返回值对于我们 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号