linux下线程
linux下线程
。
。。cha)
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h> pthread_once_t once = PTHREAD_ONCE_INIT; //指定參数位仅仅运行一次 void run(void) //演示样例线程函数
{
printf("function run is runing in thread %d\n",pthread_self());
} void *thread1(void *arg)
{
pthread_t thid = pthread_self(); //接受而且打印当前线程的ID
printf("current thread id is %d\n",thid); //调用运行线程函数仅仅运行一次,接受一个标志參数和一个目标函数的指针
pthread_once(&once,run);
printf("thread1 ends\n"); //打印函数调用结束提示信息
} void *thread2(void *arg)
{
pthread_t thid = pthread_self(); //接受而且打印当前线程ID
printf("current thread is ID %u\n",thid);
pthread_once(&once,run); //调用函数同上以一个函数结构。可是这里并不会成功调用由于仅仅运行一次
printf("thread2 ends\n"); //打印函数调用结束信息
}
int main()
{
pthread_t thid1,thid2; //定义两个线程ID
pthread_create(&thid1,NULL,thread1,NULL); //创建线程。调用上边的函数1和函数2
pthread_create(&thid2,NULL,thread2,NULL);
sleep(3); //主线程(进程)暂停3秒后继续运行
printf("main thread exit!\n");
exit(0);
}
run 函数在线程thread1 中仅仅执行了一次。尽管thread2也调用了,然而并没有什么用
typedef struct
{
int detachstate; 线程的分离状态
int schedpolicy; 线程调度策略
struct sched_param schedparam; 线程的调度參数
int inheritsched; 线程的继承性
int scope; 线程的作用域
size_t guardsize; 线程栈末尾的警戒缓冲区大小
int stackaddr_set;
void * stackaddr; 线程栈的位置
size_t stacksize; 线程栈的大小
}pthread_attr_t;
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h> void assisthread(void *arg) //演示样例线程函数
{
printf("i am nothing helping to do some thing\n");
sleep(3); //暂停三秒
//pthread_exit(0);
//线程结束
exit(0);
} int main()
{
pthread_t assisthid;
int status ; pthread_create(&assisthid,NULL,(void *)assisthread,NULL); //创建线程
pthread_join(assisthid,(void *)&status); //等待指定线程结束
printf("assistthread's exit is caused %d\n",status); return 0;
私有数据:
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h> pthread_key_t key; //定义全局变量,键 void *thread2(void *arg) //第二个函数
{
int tsd = 5; //自己线程的私有数据
printf("thread %d is runing \n",pthread_self());
pthread_setspecific(key,(void *)tsd); //设置一个私有数据
printf("thread %d return %d\n",pthread_self(),pthread_getspecific(key));
} void *thread1(void *arg)//创建线程一然后调用函数二创建还有一个线程
{
int tsd = 0;
pthread_t thid2; printf("thread %d is runing\n",pthread_self());
pthread_setspecific(key,(void *)tsd);
pthread_create(&thid2,NULL,thread2,NULL);
sleep(5);
printf("thread %d return %d\n",pthread_self(),pthread_getspecific(key));
} int main()
{
pthread_t thid1;
printf("main thread begins running\n");
pthread_key_create(&key,NULL); //创建一个键
pthread_create(&thid1,NULL,thread1,NULL); //调用函数一创建线程1
sleep(3);
pthread_key_delete(key); //删除键
printf("main thread exit\n");
return 0;
}
终于打印的值一个是5 一个是0,足见这是各个线程私有的数据。
linux下线程的更多相关文章
- Linux 下线程的理解
2017-04-03 最近深入研究了下Linux线程的问题,发现自己之前一直有些许误解,特记之…… 关于Linux下的线程,各种介绍Linux的书籍都没有深入去解释的,或许真的如书上所述,Linux本 ...
- linux下线程调用sleep,进程挂起
http://blog.csdn.net/horstlinux/article/details/7911457 http://blog.csdn.net/eroswang/article/detail ...
- linux下线程的两种封装方式
在网络编程的时候往往需要对Linux下原生的pthread库中的函数进行封装,使其使用起来更加方便,封装方法一般有两种:面向对象和基于对象,下面将分别介绍这两种方式,最后统一分析这两种方式的优缺点: ...
- Linux下线程同步的几种方法
Linux下提供了多种方式来处理线程同步,最常用的是互斥锁.条件变量和信号量. 一.互斥锁(mutex) 锁机制是同一时刻只允许一个线程执行一个关键部分的代码. 1. 初始化锁 int pthrea ...
- linux下线程调试 ulimit core
在linux 下写线程程序的同学预计都遇到过找bug找到崩溃的情况.多线程情况下bug的追踪实在是不easy. 如今我来介绍一个好用的方法 ulimit core. 先简介一下ulimit是个什么(你 ...
- Linux下线程池的理解与简单实现
首先,线程池是什么?顾名思义,就是把一堆开辟好的线程放在一个池子里统一管理,就是一个线程池. 其次,为什么要用线程池,难道来一个请求给它申请一个线程,请求处理完了释放线程不行么?也行,但是如果创建线程 ...
- linux下线程的分离和结合属性
在任何一个时间点上,线程是可结合的(joinable),或者是分离的(detached).一个可结合的线程能够被其他线程收回其资源和杀死:在被其他线程回收之前,它的存储器资源(如栈)是不释放的.相反, ...
- linux 下线程错误查找,与线程分析命令
一. 使用top和jstack查找线程错误 我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu ...
- [转] unix/linux下线程私有数据实现原理及使用方法
在维护每个线程的私有数据的时候,我们可能会想到分配一个保存线程数据的数组,用线程的ID作为数组的索引来实现访问,但是有一个问题是系统生成的线程 ID不能保证是一个小而连续的整数,并且用数组实现的时候 ...
随机推荐
- flume採集数据导入elasticsearch 配置
Flume启动通常会报两种错,一种是log4j没有配置,第二种就是缺少各种jar包.SO: [root@laiym ~]# cp /usr/local/elasticsearch/lib/*/usr/ ...
- VMware-workstation安装
下载:百度搜索VMware-workstation 开始安装:VMware-workstation-full_12.5.5.17738 更改安装目录F:\softwore\VMware\VMware ...
- Unity3D摄像机尾随人物
这里的镜头主要是从人物的背后尾随的. 首先新建一个C#脚本,命名为MyFollow,然后把下面代码粘贴进去.保存: using UnityEngine; using System.Collection ...
- java基础——transient
今天在看struts1源代码的时候,发如今ActionForm中首先声明了两个transient类型的protected变量. 之前没有接触过该transient类型,所以就查了查. transien ...
- Windows 10 游戏录制工具栏
- ABBYY简体中文版终身授权半价来袭,真的是5折!
经过了一个春秋,心心念念的双十一终于要来了,一年时间并不长,但这一个月尤其慢!ABBYY官方称为回馈广大用户的支持与厚爱,双十一期间,ABBYY价格感人,诱惑难挡. 说到双十一活动,方式也是五花八门, ...
- Java用freemarker导出Word 文档
1.用Microsoft Office Word打开word原件: 2.把需要动态修改的内容替换成***,如果有图片,尽量选择较小的图片几十K左右,并调整好位置: 3.另存为,选择保存类型Word 2 ...
- Redis数据库入门基础,及优缺点介绍
简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis 是一个高性能的key-value数据库.R ...
- Server初见——python
import socketphone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)phone.bind(('127.0.0.1',8080))p ...
- failed to push some refs to 'git@github.com:RocsSun/mytest.git
Git推送到GitHub仓库失败 使用Git将文件推送至GitHub的远程仓库时,报错failed to push some refs to 'git@github.com:RocsSun/mytes ...