linux线程(一)基本应用
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *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);
int pthread_join(pthread_t thread, void **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);
#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线程(一)基本应用的更多相关文章
- [转载]Linux 线程实现机制分析
本文转自http://www.ibm.com/developerworks/cn/linux/kernel/l-thread/ 支持原创.尊重原创,分享知识! 自从多线程编程的概念出现在 Linux ...
- linux线程的实现
首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个分身可以处理一件特定事情.这在处理异步事件如异步IO时特别有用.内核线程的使用是廉价的,唯一使用 ...
- linux线程的实现【转】
转自:http://www.cnblogs.com/zhaoyl/p/3620204.html 首先从OS设计原理上阐明三种线程:内核线程.轻量级进程.用户线程 内核线程 内核线程就是内核的分身,一个 ...
- Linux线程-创建
Linux的线程实现是在内核以外来实现的,内核本身并不提供线程创建.但是内核为提供线程[也就是轻量级进程]提供了两个系统调用__clone()和fork (),这两个系统调用都为准备一些参数,最终都用 ...
- Linux线程学习(一)
一.Linux进程与线程概述 进程与线程 为什么对于大多数合作性任务,多线程比多个独立的进程更优越呢?这是因为,线程共享相同的内存空间.不同的线程可以存取内存中的同一个变量.所以,程序中的所有线程都可 ...
- Linux线程学习(二)
线程基础 进程 系统中程序执行和资源分配的基本单位 每个进程有自己的数据段.代码段和堆栈段 在进行切换时需要有比较复杂的上下文切换 线程 减少处理机的空转时间,支持多处理器以及减少上下文切换开销, ...
- Linux 线程(进程)数限制分析
1.问题来源公司线上环境出现MQ不能接受消息的异常,运维和开发人员临时切换另一台服务器的MQ后恢复.同时运维人员反馈在出现问题的服务器上很多基本的命令都不能运行,出现如下错误:2. 初步原因分析和 ...
- Linux 线程与进程,以及通信
http://blog.chinaunix.net/uid-25324849-id-3110075.html 部分转自:http://blog.chinaunix.net/uid-20620288-i ...
- linux 线程的内核栈是独立的还是共享父进程的?
需要考证 考证结果: 其内核栈是独立的 206 static struct task_struct *dup_task_struct(struct task_struct *orig) 207 { 2 ...
- Linux 线程模型的比较:LinuxThreads 和 NPTL
Linux 线程模型的比较:LinuxThreads 和 NPTL GNU_LIBPTHREAD_VERSION 宏 大部分现代 Linux 发行版都预装了 LinuxThreads 和 NPTL,因 ...
随机推荐
- Cisco 交换机与路由器故障处理方法分享
一.网络的复杂性 一般网络包括路由.拨号.交换.视频.WAN(ISDN.帧中继.ATM.…).LAN.VLAN.… 二.故障处理模型 1. 界定问题(Define the Problem) 详细而精确 ...
- virtualization-vs-containerization
http://containerz.blogspot.com/2015/03/virtualization-vs-containerization.html Virtualization vs. Co ...
- c语言冒泡排序,指针,数组
冒泡排序算法的运作如下: 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 针对所有的元素重复 ...
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
今天把notice表中的content字段改为long字段后,含有该字段的使用CTE的查询语句报错了.提示一下错误 ### Cause: java.sql.SQLException: 流已被关闭 ; ...
- Hadoop的伪分布式搭建
我们在搭建伪分布式Hadoop环境,需要将一系列的配置文件配置好. 一.配置文件 1. 配置文件hadoop-env.sh export JAVA_HOME=/opt/modules/jdk1.7.0 ...
- Apache Commons Beanutils对象属性批量复制(pseudo-singleton)
Apache Commons Beanutils为开源软件,可在Apache官网http://commons.apache.org/proper/commons-beanutils/download_ ...
- 虚拟机及ubuntu环境搭建问题
1.现象: VMware中ubuntu ping通 宿主机windows VMware中ubuntu ping通 百度 宿主机windows ping不通 VMware中ubuntu 解决办法: 确保 ...
- WPF动画之关键帧动画(2)
XAML代码: <Window x:Class="关键帧动画.MainWindow" xmlns="http://schemas.microsoft.com/win ...
- 动态规划&矩阵连乘
动态规划&矩阵连乘 动态规划的概念 • 与分治方法类似 分-治-合 • 与分治方法不同 子问题之间并非相互独立 • 基本思想 用一个表记录 ...
- IDC机房动力环境设备维护
高低压配电 空调 ...