1.进程与线程

  1)用户空间角度:

  进程:fork()创建进程,在创建时,重新申请了内存空间,copy了父进程的所有信息。

  线程:pthread_create()创建进程时,只申请自己的栈空间。

  2)内核空间:

  对内核空间,两者都有自己的pid,因此内核空间不区分。

2.基本函数:

  1)创建线程:

  #include <pthread.h>

  extern in t pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void  *arg

  第1个参数: 线程id

  第2个参数:用来设置线程属性,通常为NULL

  第3个参数:需要创建线程的执行代码地址

  第4个参数:线程执行参数

  pthread.h支持的是POSIX,编译时加-lpthread

  1. #include<stdio.h>
  2. #include<pthread.h>
  3. #include<stdlib.h>
  4. #include<unistd.h>
  5. #include<string.h>
  6. #include<sys/syscall.h>
  7. struct data{
  8. int i;
  9. int j;
  10. };
  11. void *hello(struct data *str)
  12. {
  13. printf("child, the tid=%lu, pid=%ld\n",pthread_self(),syscall(SYS_gettid));
  14. printf("arg.i=%d\narg.j=%d \n",str->i,str->j);
  15. sleep();
  16. }
  17.  
  18. int main(int agrc,char *agrv[])
  19. {
  20. struct data test;
  21. pthread_t thread_id;
  22. test.i=;
  23. test.j=;
  24. pthread_create(&thread_id,NULL,(void*)*hello,&test);
  25. printf("parent, the tid=%lu,pid=%ld\n",pthread_self(),syscall(SYS_gettid));
  26. pthread_join(thread_id,NULL);
  27. }

  2)线程退出:

  extern void pthread_exit(void *_retval),参数为线程取消状态。

  int pthread _cancel(pthread_t thread)

  线程取消的清理:

  int pthread_cleanup_push(void (*routine)(void*),void *arg)

  int pthread_cleanup_pop(int execute)

  1. #include<pthread.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<stdio.h>
  5.  
  6. void cleanup()
  7. {
  8. printf("cleanup\n");
  9. }
  10. void *test_cancel(void)
  11. {
  12. pthread_cleanup_push(cleanup,NULL);
  13. printf("test_cancel\n");
  14. while()
  15. {
  16. printf("test message\n");
  17. sleep();
  18. }
  19. pthread_cleanup_pop();
  20. }
  21. int main()
  22. {
  23. pthread_t tid;
  24. pthread_create(&tid,NULL,(void *)test_cancel,NULL);
  25. sleep();
  26. pthread_cancel(tid);
  27. pthread_join(tid,NULL); // pthread_join()等待线程执行
  28. }

  3)线程与私有数据:

  int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))   //创建私有数据

  int pthread_key_delete(pthread_key_t key)  //删除私有数据

  int pthread_setspecific(pthread_key_t key,const void *pointer)  //读,pointer是与key相关联的

  int pthread_getspecific(pthread_key_t key)  //写

3.互斥锁:以排他的方式防止共享数据并发的访问

  1)初始化:

  #include <pthread.h>

  int pthread_mutex_init(pthread_mutex_t *restrict mutex,const pthread_mutexattr_t *restrict attr)

  第1个参数:所要指向的互斥锁指针

  第2个参数:指向属性对象的指针,如NULL使用默认属性

  2)申请互斥锁:

  int pthread_mutex_lock(pthread_mutex_t  *mutex)       //阻塞方式申请

  int pthread_mutex_trylock(pthread_mutex_t  *mutex)  //非阻塞方式申请

  3)释放互斥锁:

  int pthread_mutex_unlock(pthread_mutex_t  *mutex)

  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. #include <string.h>
  7.  
  8. void *thread_function(void *arg);
  9.  
  10. pthread_mutex_t work_mutex;
  11.  
  12. #define WORK_SIZE 1024
  13. char work_area[WORK_SIZE];
  14. int time_to_exit = ;
  15.  
  16. int main(int argc,char *argv[])
  17. {
  18. int res;
  19. pthread_t a_thread;
  20. void *thread_result;
  21. res = pthread_mutex_init(&work_mutex, NULL); //init mutex
  22. if (res != )
  23. {
  24. perror("Mutex initialization failed");
  25. exit(EXIT_FAILURE);
  26. }
  27. res = pthread_create(&a_thread, NULL, thread_function, NULL);//create new thread
  28. if (res != )
  29. {
  30. perror("Thread creation failed");
  31. exit(EXIT_FAILURE);
  32. }
  33. pthread_mutex_lock(&work_mutex); //lock the mutex
  34. printf("Input some text. Enter 'end' to finish\n");
  35. while(!time_to_exit)
  36. {
  37. fgets(work_area, WORK_SIZE, stdin); //get a string from stdin
  38. pthread_mutex_unlock(&work_mutex); //unlock the mutex
  39. while()
  40. {
  41. pthread_mutex_lock(&work_mutex); //lock the mutex
  42. if (work_area[] != '\0')
  43. {
  44. pthread_mutex_unlock(&work_mutex); //unlock the mutex
  45. sleep();
  46. }

lunux多线程编程的更多相关文章

  1. Web Worker javascript多线程编程(一)

    什么是Web Worker? web worker 是运行在后台的 JavaScript,不占用浏览器自身线程,独立于其他脚本,可以提高应用的总体性能,并且提升用户体验. 一般来说Javascript ...

  2. Web Worker javascript多线程编程(二)

    Web Worker javascript多线程编程(一)中提到有两种Web Worker:专用线程dedicated web worker,以及共享线程shared web worker.不过主要讲 ...

  3. windows多线程编程实现 简单(1)

    内容:实现win32下的最基本多线程编程 使用函数: #CreateThread# 创建线程 HANDLE WINAPI CreateThread( LPSECURITY_ATTRIBUTES lpT ...

  4. Rust语言的多线程编程

    我写这篇短文的时候,正值Rust1.0发布不久,严格来说这是一门兼具C语言的执行效率和Java的开发效率的强大语言,它的所有权机制竟然让你无法写出线程不安全的代码,它是一门可以用来写操作系统的系统级语 ...

  5. windows多线程编程星球(一)

    以前在学校的时候,多线程这一部分是属于那种充满好奇但是又感觉很难掌握的部分.原因嘛我觉得是这玩意儿和编程语言无关,主要和操作系统的有关,所以这部分内容主要出现在讲原理的操作系统书的某一章,看完原理是懂 ...

  6. Java多线程编程核心技术---学习分享

    继承Thread类实现多线程 public class MyThread extends Thread { @Override public void run() { super.run(); Sys ...

  7. python多线程编程

    Python多线程编程中常用方法: 1.join()方法:如果一个线程或者在函数执行的过程中调用另一个线程,并且希望待其完成操作后才能执行,那么在调用线程的时就可以使用被调线程的join方法join( ...

  8. 浅述WinForm多线程编程与Control.Invoke的应用

    VS2008.C#3.0在WinForm开发中,我们通常不希望当窗体上点了某个按钮执行某个业务的时候,窗体就被卡死了,直到该业务执行完毕后才缓过来.一个最直接的方法便是使用多线程.多线程编程的方式在W ...

  9. Java—多线程编程

    一个多线程程序包含两个或多个能并发运行的部分.程序的每一部分都称作一个线程,并且每个线程定义了一个独立的执行路径. 进程:一个进程包括由操作系统分配的内存空间,包含一个或多个线程.一个线程不能独立的存 ...

随机推荐

  1. 斜率dp+cdq分治

    写在前面 这个东西应该是一个非常重要的套路......所以我觉得必须写点什么记录一下,免得自己忘掉了 一直以来我的斜率dp都掌握的不算很好......也很少主动地在比赛里想到 写这个的契机是noi.a ...

  2. visio应用程序相关设置-选项-视图

    1.是否显示"新建"选项卡,可读/写 ApplicationSettings.ShowChooseDrawingTypePane m_Visio.Window.Applicatio ...

  3. C++——内存使用

    内存分配方式: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建.在执行函数时,函数内局部变量的存储单 ...

  4. 如何用Ajax传一个数组数据

    PHP接收多个同名复选框信息不像ASP那样自动转换成为数组,这给使用带来了一定不便.但是还是有解决办法的,就是利用javascript做一下预处 理.多个同名复选框在javascript中还是以数组的 ...

  5. [ZJOI2007]棋盘制作 (单调栈)

    [ZJOI2007]棋盘制作 题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8 \times 88×8大小的黑白相间 ...

  6. 转: 构建基于Nginx的文件服务器思路与实现

    在Web项目中使用独立的服务器来保存文件和图片的好处很多,如:便于统一管理,分流web服务器的压力,可进行访问加速等.另外当web服务器需要做集群进行负载均衡时,图片和文件上传在各个服务器之间同步将是 ...

  7. java bigdemical比较大小

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_33451004/article/details/71247041 java中对bigdimic ...

  8. POJ 3070 + 51Nod 1242 大斐波那契数取余

    POJ 3070 #include "iostream" #include "cstdio" using namespace std; class matrix ...

  9. PHP等比例生成缩略图

    /** * 生成缩略图 * $imgSrc 图片源路径 * $resize_width 图片宽度 * $resize_height 图片高度 * $dstimg 缩略图路径 * $isCut 是否剪切 ...

  10. 1552: [Cerc2007]robotic sort

    这道题用splay写 先离散化数据保证按题目所述顺序来写 按原序作为键值建树 维护区间最小值去跑 每次将i的位置 和 n的位置x和y找出来后 将x旋转到root y旋转到x的有儿子 这时y的左子树就是 ...