您的位置:首页 > 运维架构 > Linux

Linux线程(1): 线程的创建

2007-07-31 18:16 246 查看
1. 概念:

线程与进程是不同的, 准确地说, 进程可以由多个线程组成, 线程是进程的组成元素. 默认情况下, 一个进程只是由一个线程构成, 它串行执行该进程要执行的任务. 多线程情况下, 每个线程执行各自的任务, 它们之间更多是并行的. 线程的作用具体有:

在单进程环境中执行多个任务.

共享该进程的组成部件, 如文件描述符和内存, 但需要较为复杂的机制.

同步和异步执行.

线程改善吞吐量, 由串行转化为并行, 交叉执行.

线程的特征测试宏_POSIX_THREADS:

用#ifdef测试上述宏以确定是否支持线程.

用_SC_THREADS常数调用sysconf函数, 以确定是否支持线程

2. 线程标识(ID):

线程有ID, 但不是系统唯一, 而是进程环境中唯一有效.

线程的句柄是pthread_t类型, 该类型不能作为整数处理, 而是一个结构.

下面介绍两个函数:

头文件: <pthread.h>

原型: int pthread_equal(pthread_t tid1, pthread_t tid2);

返回值: 相等返回非0, 不相等返回0.

说明: 比较两个线程ID是否相等.

头文件: <pthread.h>

原型: pthread_t pthread_self();

返回值: 返回调用线程的线程ID.

3. 线程创建:

在执行中创建一个线程, 可以为该线程分配它需要做的工作(线程执行函数), 该线程共享进程的资源. 创建线程的函数pthread_create()

头文件: <pthread.h>

原型: int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(start_rtn)(void), void *restrict arg);

返回值: 成功则返回0, 否则返回错误编号.

参数:

tidp: 指向新创建线程ID的变量, 作为函数的输出.

attr: 用于定制各种不同的线程属性, NULL为默认属性.

start_rtn: 函数指针, 为线程开始执行的函数名.

arg: 函数的唯一无类型(void)指针参数, 如要传多个参数, 可以用结构封装.

4. 实例:

main.c代码:


#include <pthread.h>






pthread_t ntid; /**//* save thread ID */






/**//* print process ID and thread ID */


void printids(const char *s)




...{


pid_t pid;


pthread_t tid;




pid = getpid();


tid = pthread_self();


printf(%s pid %d tid %d (0x%x) ", s, pid, tid, tid);


}






/**//* thread process func */


void *thread_proc(void *arg)




...{


printids("new thread: ");


return NULL;


}






/**//* main func */


int main()




...{


int err;






/**//* create a thread */


err = pthread_create(&ntid, NULL, thread_proc, NULL);






/**//* error handler */


if (err != 0)


perror("can't create thread");




printids("main thread: ");


sleep(1);




return 0;


}

编译命令:


gcc -o test main.c -lpthread

在这个例子中, 可能会有人疑问为什么在main中加上sleep, 这是因为主线程需要休眠, 否则可能会比新线程更早退出, 这个在新线程运行前整个进程可能已经终止了. 所以加上sleep让主线程休眠一下, 确保新线程先完成.

在你的Linux中跑一下这个代码, 看看线程ID有什么特点.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: