有感而发(可以直接忽略~):每次要用到线程,都要在网上重新学下基础,例子倒是不少:一种是排版好,讲的不全又不是自己想要的;一种是排版不好,直接略过了。两者兼有的又要苦苦寻找,所以还是自己总结了,觉得每个程序员都得了一种看别人不顺眼的病,哈哈。希望大家批评指正,我这个排版和总结有什么可优化的,绝对尽力而为。
本文主要介绍linux下线程的基本应用,列举了几个常用函数的用法及实例。
头文件 pthread.h
编译选项需要加 -pthread
 
线程创建函数原型:
 int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg);
参数说明:
thread:线程ID
attr:线程属性,通常设为NULL(用到其他属性,再来补充)
start_route():线程入口函数
arg:线程入口函数参数
例:

 #include <stdio.h>
#include <unistd.h>
#include <pthread.h> void thread_function(void *param)
{
printf("this is a thread.\n");
}
int thread_test(void)
{
pthread_t thread_id;
int ret;
ret = pthread_create(&thread_id, NULL, (void *)&thread_function, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
} /*wait thread exit.*/
sleep();
return ;
}
int main(int argc, char *argv[])
{
thread_test();
return ;
}

此代码中存在一处内存泄漏问题,详情点击此处跳转

线程退出函数原型:

 void pthread_exit(void *retval);
说明:
用于线程的主动退出,与return作用基本相同,但pthread_cleanup_push()和pthread_cleanup_pop()不接收return返回值。
若要在线程中终止另一个线程,需要用pthread_cancel();
参数说明:
retval:线程结束时的返回值,可由其他函数获取,如pthread_join()。
 
等待线程函数原型:
 int pthread_join(pthread_t thread, void **retval);
说明:
等待线程退出,在等待期间当前线程将被挂起。
参数说明:
thread:等待线程的ID
retval:等待线程的返回值
例:

 #include <stdio.h>
#include <unistd.h>
#include <pthread.h>
void thread_function(void *param)
{
printf("this is a thread.\n");
sleep();
pthread_exit((void *));
}
int thread_test(void)
{
pthread_t thread_id;
int ret;
int *retval;
ret = pthread_create(&thread_id, NULL, (void *)&thread_function, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
}
ret = pthread_join(thread_id, (void *)&retval);
if(ret != ) {
printf("pthread_join fail\n");
return -;
}
printf("thread return value is %d\n", retval);
}
int main(int argc, char *argv[])
{
thread_test();
return ;
}
 
取消线程函数原型:
 int pthread_cancel(pthread_t thread);
说明:
在线程中终止另一个进程。
线程可以设置状态,来决定是否可以被其他线程取消(pthread_setcancelstate),默认可以被取消。
参数说明:
thread:要取消线程的ID
例:

 #include <stdio.h>
#include <unistd.h>
#include <pthread.h>
pthread_t thread1_id, thread2_id;
void thread_function1(void *param)
{
printf("this is a thread 2.\n");
while() {
printf("thread 1 is running.\n");
sleep();
}
printf("thread 1 exit.\n");
}
void thread_function2(void *param)
{
printf("this is a thread 2.\n");
sleep();
printf("thread 2 cancel thread 1.\n");
pthread_cancel(thread1_id);
}
int thread_test(void)
{
int ret;
ret = pthread_create(&thread1_id, NULL, (void *)&thread_function1, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
}
ret = pthread_create(&thread1_id, NULL, (void *)&thread_function2, NULL);
if(ret != ) {
printf("pthread_create fail\n");
return -;
}
ret = pthread_join(thread1_id, NULL);
if(ret != ) {
printf("pthread_join fail\n");
return -;
}
}
int main(int argc, char *argv[])
{
thread_test();
return ;
}

只是简单的介绍了下线程的基本操作,更高级的应用稍后更新敬请期待~~

linux线程(一)基本应用的更多相关文章

  1. [转载]Linux 线程实现机制分析

    本文转自http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 支持原创.尊重原创,分享知识! 自从多线程编程的概念出现在 Linux ...

  2. linux线程的实现

    首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个分身可以处理一件特定事情.这在处理异步事件如异步IO时特别有用.内核线程的使用是廉价的,唯一使用 ...

  3. linux线程的实现【转】

    转自:http://www.cnblogs.com/zhaoyl/p/3620204.html 首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个 ...

  4. Linux线程-创建

    Linux的线程实现是在内核以外来实现的,内核本身并不提供线程创建.但是内核为提供线程[也就是轻量级进程]提供了两个系统调用__clone()和fork (),这两个系统调用都为准备一些参数,最终都用 ...

  5. Linux线程学习(一)

    一.Linux进程与线程概述 进程与线程 为什么对于大多数合作性任务,多线程比多个独立的进程更优越呢?这是因为,线程共享相同的内存空间.不同的线程可以存取内存中的同一个变量.所以,程序中的所有线程都可 ...

  6. Linux线程学习(二)

    线程基础 进程 系统中程序执行和资源分配的基本单位 每个进程有自己的数据段.代码段和堆栈段 在进行切换时需要有比较复杂的上下文切换   线程 减少处理机的空转时间,支持多处理器以及减少上下文切换开销, ...

  7. Linux 线程(进程)数限制分析

    1.问题来源公司线上环境出现MQ不能接受消息的异常,运维和开发人员临时切换另一台服务器的MQ后恢复.同时运维人员反馈在出现问题的服务器上很多基本的命令都不能运行,出现如下错误:2.   初步原因分析和 ...

  8. Linux 线程与进程,以及通信

    http://blog.chinaunix.net/uid-25324849-id-3110075.html 部分转自:http://blog.chinaunix.net/uid-20620288-i ...

  9. linux 线程的内核栈是独立的还是共享父进程的?

    需要考证 考证结果: 其内核栈是独立的 206 static struct task_struct *dup_task_struct(struct task_struct *orig) 207 { 2 ...

  10. Linux 线程模型的比较:LinuxThreads 和 NPTL

    Linux 线程模型的比较:LinuxThreads 和 NPTL GNU_LIBPTHREAD_VERSION 宏 大部分现代 Linux 发行版都预装了 LinuxThreads 和 NPTL,因 ...

随机推荐

  1. 容器大小的改变以及容器操作可能使迭代器失效、vector对象的容量变化

    1 改变容器的大小 我们可以使用resize来增加或缩小容器,与往常一样,array不支持resize.如果当前大小大于所要求的大小,容器后面的元素会被删除:如果当前大小小于新大小,会将新元素添加到容 ...

  2. PullToRefreshScrollView 修改下拉刷新图标

    我的修改比较简单暴力.网上查了一番,貌似大家都没有改,无奈,查了一下源码.发现如下资源目录: 在看看我们的布局文件,此三个图片就是下拉刷新的三种图标 好吧,flip就是我目前的下拉刷新图片,对应的也就 ...

  3. 解决ubuntu16.04下的sublime text3不能使用Fcitx下的搜狗输入法的问题

    Sublime Text 2/3 输入法(Fcitx)修复[Ubuntu(Debian)] 主要目的 安装 Sublime Text 3 安装 Fcitx 输入法 + 皮肤 修复 Sublime Te ...

  4. 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题

    今天把notice表中的content字段改为long字段后,含有该字段的使用CTE的查询语句报错了.提示一下错误 ### Cause: java.sql.SQLException: 流已被关闭 ; ...

  5. A题笔记(13)

    Evaluate Reverse Polish Notation Reverse Words in a String 类似的,需要将原序列后序排列的时候,都可以用 栈 来实现 stack<int ...

  6. CSS3 transition-timing-function

    CSS3 transition-timing-function 属性 定义和用法 transition-timing-function 属性规定过渡效果的速度曲线. 该属性允许过渡效果随着时间来改变其 ...

  7. bzoj1007:[HNOI2008]水平可见直线

    思路:首先按斜率排序,如果斜率相同就取截距最大的,显然截距小的会被覆盖而对答案没有贡献,然后考虑斜率不同的如何统计答案,可以用一个单调栈维护,当前新插入的直线显然斜率是要比当前栈顶斜率要大的,然后如果 ...

  8. 基于ACE的定时器模板类

    ACETimerClockGenerator.h ClockGeneratorIF.h 在类中定义一个结构体,在结构体中定义一个函数. 在结构体中定义一个函数,这样做有什么好呢? TimerHandl ...

  9. 一次ora-1113 记录

    记录博客园的第一天,今天在电脑前发呆,突然感觉自己记忆越来越差,近年来随着工作力度的加强,感觉自己越来越力不从心,问题重复的出现.感觉自己应该去记录点什么了,随选择了用写博客的方式记录一下.第一天先记 ...

  10. SQL Cursor(游标)

    1.游标在数据表没有id(identity(1,1))时好用,但是游标会吃更多的内存,减少可用的并发,占用宽带,锁定资源,当然还有更多的代码量 2.如果能不用游标,尽量不要使用游标,用完用完之后一定要 ...