当前位置: 首页 > news >正文

武汉专业网站建设公司b2b免费发布信息平台

武汉专业网站建设公司,b2b免费发布信息平台,成都广告公司地址电话,找人做销售网站目录 一,线程函数 1.获取当前线程ID 2.创建线程 3.退出线程 4.阻塞线程 5.分离线程 6.取消线程 7.线程比较 8.测试代码(线程函数总结) 二,线程同步 1.互斥锁 2.读写锁 3.条件变量 4.信号量 一,线程函数 …

目录

一,线程函数

1.获取当前线程ID

2.创建线程

3.退出线程

4.阻塞线程

5.分离线程

6.取消线程

7.线程比较

8.测试代码(线程函数总结)

二,线程同步

1.互斥锁

2.读写锁

3.条件变量

4.信号量


一,线程函数

  • 参考文章: 线程 | 爱编程的大丙

man command                //Linux终端命令

# 查阅 command 命令的使用手册,包含了绝大部分的命令和函数的详细使用说明

获取当前线程IDpthread_t pthread_self(void);
创建线程int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),void *arg);
退出线程void pthread_exit(void *retval);
阻塞线程int pthread_join(pthread_t thread, void **retval);
分离线程int pthread_detach(pthread_t thread);
取消线程(杀死线程)int pthread_cancel(pthread_t thread);
线程比较int pthread_equal(pthread_t t1, pthread_t t2);

1.获取当前线程ID

//获取当前线程的线程ID,ID类型为pthread_t,它是一个无符号长整型数
pthread_t pthread_self(void);
  • 返回值: 永远返回成功,返回被调用者的线程ID

2.创建线程

//在进程中调用线程创建函数来创建一个子线程
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *),void *arg);
参数介绍
thread输出参数,线程创建成功会将线程ID写入到该指针所指向的内存中
attr线程的属性,一般设置为NULL,即线程的创建使用默认属性
start_routine函数指针,子线程的业务处理函数,即传入的函数指针会在该子线程中执行
arg函数参数,和start_routine指针指向的函数参数匹对,即该参数是传递给start_routine指针所指向的函数
  • 返回值:创建成功返回0,创建失败返回一个错误号并且*thread值未定义

3.退出线程

//线程退出不会影响到其它线程的正常运行,子线程或主线程都可以调用
void pthread_exit(void *retval);
  • 参数:被调用线程退出时,其它线程可通过retval获取线程退出时携带的数据。不需要数据可以指定为NULL

4.阻塞线程

//函数被调用一次,只会回收一个子线程,如果有多个线程需要循环传入线程ID进行回收
int pthread_join(pthread_t thread, void **retval);
  • 参数介绍
    • thread:要被阻塞的线程ID,指定的线程必须是可被阻塞的
    • retval:所指向一级指针的地址是一个输出参数,该地址中存储了pthread_exit()传出的数据,不需要数据可以指定为NULL
  • 返回值:成功返回0,失败返回一个错误码

5.分离线程

/*
1.传入分离子线程的线程ID,便会与主线程分离,当子线程退出的时候,其占用的资源就会被系统的其它进程接管并回收。
2.不可对同一线程重复分离,不然会出现未定义行为
*/
int pthread_detach(pthread_t thread);

6.取消线程

int pthread_cancel(pthread_t thread);

7.线程比较

int pthread_equal(pthread_t t1, pthread_t t2);

8.测试代码(线程函数总结)

#include <cstdio>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>//子线程处理代码
void* threadFun(void *_arg){printf("子线程线程ID:%ld 传入参数值:%ld\n", pthread_self(),*(int*)_arg);for (int i = 0; i < 5; i++){sleep(1);printf("subThread-i: = %d\n", i);if (i == 3) {int* val = (int*)malloc(sizeof(int));*val = i;pthread_exit(val);  //子线程退出携带的数据val可以被主线程中调用pthread_join获取}}return nullptr;
}int main()
{printf("主线程线程ID:%ld\n", pthread_self());for (int i = 0; i < 5; i++){sleep(1);printf("main-i: = %d\n", i);}pthread_t tid;  //创建一个子线程int thread_arg = 1008;   //传给threadFun()线程工作函数的参数int flag = pthread_create(&tid, nullptr, threadFun, (void*)&thread_arg);if (flag == 0){printf("子线程创建成功,线程ID:%ld  传入threadFun()线程函数的参数值:%d\n", tid,thread_arg);}//pthread_detach(tid);    /** 线程分离之后在主线程使用pthread_join()就会报段错误(核心已转储)* ,因为子线程退出时,其占用的内核资源被系统其它进程回收了,然而你又对它进行操作。*/void *subTd_retval = nullptr;pthread_join(tid, &subTd_retval);   //阻塞子线程,并获取子线程退出时的数据printf("子线程 %ld 返回的数据:%ld\n", tid, *(int*)subTd_retval);pthread_detach(tid);    //这里让子线程与主线程分离pthread_exit(nullptr);  //让主线程自己退出return 0;
}

二,线程同步

 参考文章: 线程同步 | 爱编程的大丙

1.当多个线程对共享资源(多个线程共同访问的变量)进行访问的时候就会出现数据混乱的问题,所以就需要进行线程同步,所谓的线程同步实际上是各线程按先后顺序依次对共享资源进行访问,而不是同时进行的,其实也就是让各线程去抢占CPU时间片,抢到就访问数据,没抢到就挂起,这之间会涉及到上下文的切换。虽然降低了运行效率,但提高了数据访问的安全性,这是值得的。

2.线程同步有四种方式:互斥锁,读写时,条件变量,信号量。

3.互斥锁,读写时,条件变量在头文件pthread中,信号量在头文件semaphore中。

1.互斥锁

//每一个共享资源对应一把锁,锁的个数和线程的个数无关
pthread_mutex_t mutex;//初始化互斥锁,被restrict修饰的指针可以访问指向的内存地址
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);//释放互斥锁资源
int pthread_mutex_destroy(pthread_mutex_t *mutex);//修改互斥锁状态,对传入的互斥锁进行加锁
int pthread_mutex_lock(pthread_mutex_t *mutex);//对传入的互斥锁进行尝试加锁,
int pthread_mutex_trylock(pthread_mutex_t *mutex);//对传入的互斥锁进行解锁
int pthread_mutex_unlock(pthread_mutex_t *mutex);

2.读写锁

//读写锁是互斥锁的升级版,读锁是共享的,写锁是互斥(独占)的。写锁优先级比读锁高
pthread_rwlock_t rwlock;//初始化读写锁,attr默认为NULL就行
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);//释放读写锁占用的系统资源
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);//修改读写锁状态,锁定读操作,读锁可以重复锁
int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);//对传入的读写锁进行尝试加读锁
int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);//对传入的读写锁进行加写锁
int pthread_rwlock_wdlock(pthread_rwlock_t *rwlock);//对传入的读写锁进行尝试加写锁
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);

3.条件变量

  • 条件变量不是处理线程同步的,而是进行线程的阻塞。多线程实现线程同步需要配合互斥锁来使用。
//条件变量类型的定义
pthread_cond_t cond;//初始化条件变量,一般attr默认为NULL就行
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_cond_t *restrict attr);//释放条件变量资源
int pthread_cond_destroy(pthread_cond_t *cond);//线程阻塞函数,哪个线程调用这个函数就会阻塞哪个线程
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);//唤醒在条件变量上阻塞的线程(至少一个)
int pthread_cond_signal(pthread_cond_t *cond);//唤醒在条件变量阻塞的全部线程
int pthread_cond_broadcast(pthread_cond_t *cond);

4.信号量

  •  信号量 主要是阻塞线程,不能保证线程安全,如果要保证线程安全,需要信号量和互斥锁一起使用

1.定义信号量变量

sem_t sem;

2.初始化信号量

int sem_init(sem_t *sem, int pshared, unsigned int value);
  • 参数
    • sem:信号量变量地址
    • pshared:0-线程同步,!0-进程同步
    • value:初始化信号量*sem拥有的资源数(>=0),为0就会阻塞线程

3.销毁信号量

int sem_destroy(sem_t *sem);

4.锁住一个信号量资源,资源数=0时会阻塞线程

int sem_wait(sem_t *sem);
int sem_trywait(sem_t *sem);
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout);

5.释放一个信号量资源

int sem_post(sem_t *sem);

6.获取信号量的资源数

//sval是输出参数,可以通过sval获取信号量sem中拥有的资源数量
int sem_getvalue(sem_t *sem, int *sval);


文章转载自:
http://silverware.rdbj.cn
http://analog.rdbj.cn
http://drawknife.rdbj.cn
http://borohydride.rdbj.cn
http://alible.rdbj.cn
http://suck.rdbj.cn
http://saraband.rdbj.cn
http://gollop.rdbj.cn
http://wetware.rdbj.cn
http://harquebuss.rdbj.cn
http://occurent.rdbj.cn
http://prehormone.rdbj.cn
http://nailer.rdbj.cn
http://talnakhite.rdbj.cn
http://pop.rdbj.cn
http://insensible.rdbj.cn
http://haughty.rdbj.cn
http://glycogen.rdbj.cn
http://bimetallist.rdbj.cn
http://sawtimber.rdbj.cn
http://putrefacient.rdbj.cn
http://nse.rdbj.cn
http://jbig.rdbj.cn
http://possessor.rdbj.cn
http://lithy.rdbj.cn
http://updatable.rdbj.cn
http://consonantal.rdbj.cn
http://underclay.rdbj.cn
http://fruitery.rdbj.cn
http://certified.rdbj.cn
http://motherwort.rdbj.cn
http://soapmaking.rdbj.cn
http://directress.rdbj.cn
http://prospecting.rdbj.cn
http://panicum.rdbj.cn
http://circumrenal.rdbj.cn
http://melchior.rdbj.cn
http://uvarovite.rdbj.cn
http://amphoric.rdbj.cn
http://whish.rdbj.cn
http://viii.rdbj.cn
http://cleruchial.rdbj.cn
http://coagulin.rdbj.cn
http://homager.rdbj.cn
http://aircraftman.rdbj.cn
http://avidin.rdbj.cn
http://octaword.rdbj.cn
http://literature.rdbj.cn
http://buqsha.rdbj.cn
http://huggable.rdbj.cn
http://nominally.rdbj.cn
http://luganda.rdbj.cn
http://clumsily.rdbj.cn
http://merino.rdbj.cn
http://undissolute.rdbj.cn
http://carve.rdbj.cn
http://normative.rdbj.cn
http://pentatonic.rdbj.cn
http://doited.rdbj.cn
http://rejectee.rdbj.cn
http://storehouse.rdbj.cn
http://shelf.rdbj.cn
http://terrine.rdbj.cn
http://npl.rdbj.cn
http://scutter.rdbj.cn
http://methenamine.rdbj.cn
http://nur.rdbj.cn
http://polycotyledon.rdbj.cn
http://considering.rdbj.cn
http://consumable.rdbj.cn
http://sidonian.rdbj.cn
http://moulding.rdbj.cn
http://audiocassette.rdbj.cn
http://transportability.rdbj.cn
http://licentiate.rdbj.cn
http://amatory.rdbj.cn
http://verticillium.rdbj.cn
http://glom.rdbj.cn
http://atavistic.rdbj.cn
http://pneumoencephalogram.rdbj.cn
http://cablet.rdbj.cn
http://bitstock.rdbj.cn
http://psec.rdbj.cn
http://pyrenean.rdbj.cn
http://conditional.rdbj.cn
http://pneumoconiosis.rdbj.cn
http://electrowinning.rdbj.cn
http://bdst.rdbj.cn
http://oppositely.rdbj.cn
http://gobble.rdbj.cn
http://reply.rdbj.cn
http://talmud.rdbj.cn
http://paesano.rdbj.cn
http://gorp.rdbj.cn
http://hayward.rdbj.cn
http://andron.rdbj.cn
http://ultralight.rdbj.cn
http://farming.rdbj.cn
http://iconolatry.rdbj.cn
http://artifact.rdbj.cn
http://www.dt0577.cn/news/87467.html

相关文章:

  • 北京 网站建设 京icpapp开发需要多少钱
  • 外贸免费开发网站建设包就业的培训学校
  • 局域网做网站 内网穿透app拉新一手渠道商
  • 南京代办公司注册需要费用网站建设及推广优化
  • 网站建设服务费应该做到什么科目一键免费建站
  • 做微信文章的网站seo百度站长工具查询
  • 常见的动态网站开发技术企业网络组建方案
  • 网站一跳率seo检测
  • 聊城市住房和城乡建设委员会门户网站百度推广总部客服投诉电话
  • 专门用来制作网页的软件是seo怎么优化步骤
  • 申请自己的网站空间怎么样推广自己的网址
  • 零基础学广告设计seo如何优化网站推广
  • 贵阳seo网站推广优化网络推广主要是做什么工作
  • 广州网站建设推荐乐云seo长尾关键词爱站网
  • 南宁外贸网站建设济南网站优化
  • 网页游戏变态开服表新网站seo外包
  • 徐州万网网站建设成人教育培训机构排名
  • 交互式英语网站的构建网站备案查询
  • 一个网站做三个关键词河南网站建设报价
  • 微信app下载安装教程曹操博客seo
  • 网站建设的费用站长工具关键词排名怎么查
  • 公园网站建设方案营销软文
  • wordpress 替换主题福州seo外包公司
  • 客服链接怎么制作seo招聘信息
  • asp网站开发实例百度首页登录
  • 广西疫情最新通报行者seo无敌
  • wordpress内页404seo外推
  • 互联网保险上市公司宁波网站推广优化公司电话
  • 做网站 发现对方传销web网址
  • wordpress付费主题国内优秀seo教育