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不能保证是一个小而连续的整数,并且用数组实现的时候 ...
随机推荐
- logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)
在清洗日志时,有一个应用场景,就是TCP输出时,须要在一个主机挂了的情况下,自已切换到下一个可用入口.而原tcp output仅支持单个目标主机设定.故本人在原tcp的基础上,开发出tcp_multi ...
- 视差滚动demo (pc)
根据设计图设定每屏的高度,js会自动缩放到全屏尺寸,效果要大尺寸才能看的出来 demo :http://runjs.cn/detail/uvizsekd <!DOCTYPE html> & ...
- lightoj--1294--Positive Negative Sign(水题,规律)
Positive Negative Sign Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu ...
- 【POJ 2311】 Cutting Game
[题目链接] http://poj.org/problem?id=2311 [算法] 博弈论——SG函数 [代码] #include <algorithm> #include <bi ...
- Java-API-POI:POI百科
ylbtech-Java-API:POI百科 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 1. ...
- 清北集训Day1T3 LYK loves jumping(期望DP)
题目描述 LYK在玩一个魔法游戏,叫做跳跃魔法. 有n个点,每个点有两个属性hi和ti,表示初始高度,和下降高度.也就是说,它初始时高度为hi,一旦LYK踩在这个点上,由于重力的影响,这个点的高度会下 ...
- 阿里云Maven中央仓库配置
方式一:统一配置 在maven安装目录/conf下的settings.xml 文件里配置mirrors的子节点,添加如下mirror <mirror> <id>alimaven ...
- gvim74 提示报错 “无法加载库python27.dll”
官方提供的gvim安装文件默认是支持python和python3两种模式的,编译时带有该选项,但并没有附带对应的运行库和运行环境.所以在本地没有安装python时直接在vim中执行 :py print ...
- RxSwift 之变换操作
https://www.aliyun.com/jiaocheng/349821.html RxSwift入坑解读-你所需要知道的各种概念 http://www.open-open.com/lib/vi ...
- Unity 组件的增、查、禁、删 代码书写
using UnityEngine; public class NewBehaviourScript : MonoBehaviour { // Use this for initialization ...