有感而发(可以直接忽略~):每次要用到线程,都要在网上重新学下基础,例子倒是不少:一种是排版好,讲的不全又不是自己想要的;一种是排版不好,直接略过了。两者兼有的又要苦苦寻找,所以还是自己总结了,觉得每个程序员都得了一种看别人不顺眼的病,哈哈。希望大家批评指正,我这个排版和总结有什么可优化的,绝对尽力而为。
本文主要介绍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. bash shell for循环1到100 .

    前言 用bash shell写程序时,经常会用到for循环,特别是从1到100这种需求,这里记录几种shell中从1到100的循环方法   方法 类c语言 for ((i=1; i<=100;  ...

  2. VS的一部分快捷键

    快捷键                                         功能CTRL + SHIFT + B                        生成解决方案CTRL + F ...

  3. ie各个版本hack

    /*类内部hack:*/ .header {_width:100px;} /* IE6专用*/ .header {*+width:100px;} /* IE7专用*/ .header {*width: ...

  4. react环境搭建

    react-webpack文件夹是开发目录,在此目录下执行命令,假设你已经正确安装了 nodejs 一:参照教程搭建环境 https://github.com/newtriks/generator-r ...

  5. LoadRunner安装包(性能测试工具分享)

    今天在测试课指导老师朱香元的指导下,开始了测试软件的安装,下面我分享一下整个安装流程,最后我会附带安装包的 第一步:链接:http://pan.baidu.com/s/1pXqk2 密码:csjk , ...

  6. 图灵API

    namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine( ...

  7. Tomcat6.0数据库连接池配置

    http://blog.163.com/magicc_love/blog/static/185853662201111101130969/ oracle驱动包Tomcat 6.0配置oracle数据库 ...

  8. php常用正则表达式函数

    执行一个正则表达式匹配: preg_match($pattern, $subject, [array &$matches]); 最多匹配一次,返回值0或1,把第一次匹配到的结果放入$match ...

  9. Mysql JDBC 连接串参数说明

    MySQL的 JDBC URL 格式 for  Connector/J 如下例: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][= ...

  10. 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 ...