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,因 ...
随机推荐
- bash shell for循环1到100 .
前言 用bash shell写程序时,经常会用到for循环,特别是从1到100这种需求,这里记录几种shell中从1到100的循环方法 方法 类c语言 for ((i=1; i<=100; ...
- VS的一部分快捷键
快捷键 功能CTRL + SHIFT + B 生成解决方案CTRL + F ...
- ie各个版本hack
/*类内部hack:*/ .header {_width:100px;} /* IE6专用*/ .header {*+width:100px;} /* IE7专用*/ .header {*width: ...
- react环境搭建
react-webpack文件夹是开发目录,在此目录下执行命令,假设你已经正确安装了 nodejs 一:参照教程搭建环境 https://github.com/newtriks/generator-r ...
- LoadRunner安装包(性能测试工具分享)
今天在测试课指导老师朱香元的指导下,开始了测试软件的安装,下面我分享一下整个安装流程,最后我会附带安装包的 第一步:链接:http://pan.baidu.com/s/1pXqk2 密码:csjk , ...
- 图灵API
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine( ...
- Tomcat6.0数据库连接池配置
http://blog.163.com/magicc_love/blog/static/185853662201111101130969/ oracle驱动包Tomcat 6.0配置oracle数据库 ...
- php常用正则表达式函数
执行一个正则表达式匹配: preg_match($pattern, $subject, [array &$matches]); 最多匹配一次,返回值0或1,把第一次匹配到的结果放入$match ...
- Mysql JDBC 连接串参数说明
MySQL的 JDBC URL 格式 for Connector/J 如下例: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][= ...
- UVA 11995 I Can Guess the Data Structure!(ADT)
I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x T ...