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

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有什么特点.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值