C++ 多线程

一、多线程概述

在多处理器或多核系统中,可以通过并行处理提升速度。开线程相比于开进程的开销更低,因为在进程中创建线程不会涉及初始化新的虚拟内存空间以及为进程创建环境,进程内所有的线程共享相同的虚拟地址空间。

Thread operations include thread creation, termination, synchronization (joins,blocking), scheduling, data management and process interaction.

A thread does not maintain a list of created threads, nor does it know the thread that created it.

All threads within a process share the same address space.

Threads in the same process share:

  • Process instructions
  • Most data
  • open files (descriptors)
  • signals and signal handlers
  • current working directory
  • User and group id

Each thread has a unique:

  • Thread ID
  • set of registers, stack pointer
  • stack for local variables, return addresses
  • signal mask
  • priority
  • Return value: errno

参考资料:https://www.cs.cmu.edu/afs/cs/academic/class/15492-f07/www/pthreads.html

二、基本操作

2.1 POSIX thread库支持C/C++的API , 为Unix系统支持的线程操作

pthread_create

//@param thread: returns the thread id.
//@param attr : Set to NULL if default thread attributes are used, else define members of the struct pthread_attr_t defined in bits/pthreadtypes.h
//@param void * (*start_routine) : pointer to the function to be threaded. Function has a single argument: pointer to void.
//@param *arg - pointer to argument of function. To pass multiple arguments, se
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuyuelongfly

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值