学习了几天多线程技术,做个总结,便于记忆。

  一般 多线程传递参数 为 void*  所以会有一个强制转换过程  (int*) (void *)等,传递多个参数选择 结构体指针。为了避免多个线程访问数据冲突 会有一个 叫做  “临界区”CRITICALSECTION“ 类 ,防止读写数据冲突,

大概流程是:

CRITICAL_SECTION cs;

init CS(cs);

the one process

enter CS

.....

leaveCS

DELETE(cs);

在利用多线程时候,会遇到数据分割的问题 一般的规定是:

假设 data = N   process_num = M;

N 能整除M 简单  N/M

N 不能整除 M  则 M-1 个进程的数据处理数 为 N/(M-1) 最后一个进程处理数为N - (N/(M-1)*(M-1))

一般利用全局变量完成线程间的简单通信 。当然也有timer定时器控制线程启动 ,和event事件触发线程。

WINDOWS下 多线程头文件为 process.h

LINUX下     。。。               pthread.h 另外编译时候 加上 -lpthread

WINDOWS 新建一个线程 有 CreateThread _beginthread (略过参数)

LINUX下    。。。            pthread_create

WINDOWS 冻结解冻线程为 SuspendThread() ResumeThread()

LINUX下 。。。一般用线程锁  pthread_mutex_lock   pthread_mutex_unlock

  1. /*程序流程为:主线程创建子线程(当前子线程状态为stop停止状态),5秒后主线程唤醒子线程,10秒后主线程挂起子线程,15秒后主线程再次唤醒子线程,20秒后主线程执行完毕等待子线程退出。
  2.  
  3. 代码如下:*/
  4. #include "stdio.h"
  5. #include "unistd.h"
  6. #include "pthread.h"
  7. #include "string.h"
  8. #include "time.h"
  9.  
  10. #define RUN 1
  11. #define STOP 0
  12.  
  13. pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  14. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  15.  
  16. int status = STOP;
  17. void * thread_function(void)
  18. {
  19. static int i = 0;
  20. while (1)
  21. {
  22. pthread_mutex_lock(&mut);
  23. while (!status)
  24. {
  25. pthread_cond_wait(&cond, &mut);
  26. }
  27. pthread_mutex_unlock(&mut);
  28.  
  29. printf("child pthread %d\n", i++);
  30. if (i == 20)
  31. break;
  32. sleep(1);
  33. }
  34. }
  35.  
  36. void thread_resume()
  37. {
  38. if (status == STOP)
  39. {
  40. pthread_mutex_lock(&mut);
  41. status = RUN;
  42. pthread_cond_signal(&cond);
  43. printf("pthread run!\n");
  44. pthread_mutex_unlock(&mut);
  45. }
  46. else
  47. {
  48. printf("pthread run already\n");
  49. }
  50. }
  51.  
  52. void thread_pause()
  53. {
  54. if (status == RUN)
  55. {
  56. pthread_mutex_lock(&mut);
  57. status = STOP;
  58. printf("thread stop!\n");
  59. pthread_mutex_unlock(&mut);
  60. }
  61. else
  62. {
  63. printf("pthread pause already\n");
  64. }
  65. }
  66.  
  67. int main()
  68. {
  69. int err;
  70. static int i = 0;
  71. pthread_t child_thread;
  72.  
  73. #if 0
  74. if (pthread_mutex_init(&mut, NULL) != 0)
  75. printf("mutex init error\n");
  76. if (pthread_cond_init(&cond, NULL) != 0)
  77. printf("cond init error\n");
  78. #endif
  79.  
  80. err = pthread_create(&child_thread, NULL, (void *)thread_function, NULL);
  81. if (err != 0 )
  82. printf("can't create thread: %s\n", strerror(err));
  83. while(1)
  84. {
  85. printf("father pthread %d\n", i++);
  86. sleep(1);
  87. if (i == 5)
  88. thread_resume();
  89. if (i == 10)
  90. thread_pause();
  91. if (i == 15)
  92. thread_resume();
  93. if (i == 20)
  94. break;
  95. }
  96. if (0 == pthread_join(child_thread, NULL))
  97. printf("child thread is over\n");
  98. return 0;
  99. }

。。。。。涉及到线程间的同步与异步一般会用到如下函数。。。。。。。。。。。。

windows 等待线程结束  WaitForSingleObject() WaitForMultipleObjects()

Linux 。。。   pthread_join()

windows 退出线程 ExitThread() TerminateThread()/强制结束线程

linux 退出线程  pthread_exit()

还有关键的 SIGNAL 没有学习,再UPDATE吧。

最后附上linux 和windows 多线程测试代码

linux : gcc test.c -fopenmp -lpthread -o test

  1. #include "stdio.h"
  2. #include "omp.h"
  3. #include "time.h"
  4. #include "unistd.h"
  5. #include "pthread.h"
  6.  
  7. clock_t start,end;
  8.  
  9. void* test(void *p){
  10.  
  11. start = clock();
  12. int i;
  13. for(i=0;i<100000;i++)
  14. usleep(1);
  15.  
  16. end = clock();
  17.  
  18. printf("process test %d\n",end-start);
  19. return ((void *)0);
  20. }
  21.  
  22. void* test1(void *P){
  23. start = clock();
  24. int i;
  25. #pragma omp parallel for
  26. for(i = 0;i<100000;i++)
  27. usleep(1);
  28.  
  29. end = clock();
  30. printf("process test1 %d\n",end-start);
  31. return ((void *)0);
  32.  
  33. }
  34. int main(){
  35.  
  36. int err;
  37.  
  38. pthread_t ntid;
  39. pthread_t ntid1;
  40. void** out;
  41.  
  42. err = pthread_create(&ntid,0,test,0);
  43.  
  44. if(err !=0) putchar('N');
  45. err = pthread_create(&ntid1,0,test1,0);
  46.  
  47. if(err !=0) putchar('N');
  48. // test(0);
  49. // test1(0);
  50.  
  51. printf("Main process\n");
  52. pthread_join(ntid,out);
  53. pthread_join(ntid1,out);
  54.  
  55. return 0;
  56.  
  57. }

  

windows :

  1. #include <windows.h>
  2. #include <iostream>
  3. #include <process.h>
  4. #include <time.h>
  5.  
  6. #define _CRT_SECURE_NO_WARNINGS
  7. using namespace std;
  8.  
  9. CRITICAL_SECTION cs;
  10.  
  11. int i = 0;
  12.  
  13. void run(void *){
  14.  
  15. char num[30];
  16. while (1){
  17. sprintf(num,"title %d",i++);
  18. system(num);
  19. Sleep(1000);
  20. //MessageBox(0,(LPCTSTR)num,(LPCTSTR) num, 0);
  21. }
  22.  
  23. }
  24.  
  25. int main(){
  26.  
  27. int hd[4];
  28. MessageBoxA(0, "1", "1", 0);
  29. // for (int i = 0; i < 4; i++){
  30. hd[i] = _beginthread(run, 0, 0);
  31. // }
  32. WaitForSingleObject(hd, true);
  33. system("pause");
  34. return 0;
  35. }

  

参考文献:

http://www.cnblogs.com/gnuhpc/archive/2012/12/07/2807484.html

http://blog.csdn.net/zhouruifu2015/article/details/47833985

http://blog.chinaunix.net/uid-29145190-id-4341878.html

http://edu.51cto.com/lesson/id-86087.html

windows 和 linux 多线程的更多相关文章

  1. windows与linux多线程对比

      一.创建线程 1>windows HANDLE aThread[MAX_THREAD]; 函数原型: HANDLE WINAPI CreateThread( _In_opt_ LPSECUR ...

  2. [转帖]Windows和Linux对决(多进程多线程)

    Windows和Linux对决(多进程多线程) https://blog.csdn.net/world_2015/article/details/44920467 太长了 还没看完.. 还是没太理解好 ...

  3. 《Linux多线程服务端编程:使用muduo C++网络库》上市半年重印两次,总印数达到了9000册

    <Linux多线程服务端编程:使用muduo C++网络库>这本书自今年一月上市以来,半年之内已经重印两次(加上首印,一共是三次印刷),总印数达到了9000册,这在技术书里已经算是相当不错 ...

  4. 转:socket编程在windows和linux下的区别

    如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境. 下面大概分几个方面进行罗 ...

  5. socket编程在windows和linux下的区别

    如无其它说明,本文所指Linux均表示2.6内核Linux,GCC编译器,Windows均表示Windows XP系统,Visual Studio 2005 sp1编译环境. 下面大概分几个方面进行罗 ...

  6. windows和linux套接字中的select机制浅析

    先来谈谈为什么会出现select函数,也就是select是解决什么问题的? 平常使用的recv函数时阻塞的,也就是如果没有数据可读,recv就会一直阻塞在那里,这是如果有另外一个连接过来,就得一直等待 ...

  7. socket在windows下和linux下的区别

    原文:socket在windows下和linux下的区别 1)头文件 windows下winsock.h/winsock2.h linux下sys/socket.h    错误处理:errno.h 2 ...

  8. Windows和Linux下通用的线程接口

    对于多线程开发,Linux下有pthread线程库,使用起来比较方便,而Windows没有,对于涉及到多线程的跨平台代码开发,会带来不便.这里参考网络上的一些文章,整理了在Windows和Linux下 ...

  9. Windows与Linux下进程间通信技术比较

    一般我们写的程序都是以单个进程的方式来运行的,比较少涉及到多进程.特别是在windows下,因为Windows是按照线程来分配CPU时间片的,线程是最小的调度单位,所以在Windows下更多的用到多线 ...

随机推荐

  1. MUI对话框使用

    一.alert告警框 用法 .alert(message,title,btnvalue,callback[,type]); document.getElementById("noclick& ...

  2. Linux替换文件行首的空白字符

    使用命令sed.cp.tail.cat 1.拷贝一个任意文件(生产环境切勿操作) cp /etc/profile /tmp 查看文件部分格式 cat /tmp/profile # /etc/profi ...

  3. 12-python基础—python3中的reduce()

    在 Python3 中,reduce() 函数已经被从全局名字空间里移除了,它现在被放置在 functools 模块里,需要通过引入 functools 模块来调用 reduce() 函数: from ...

  4. phpstorm提示phalcon语法

    先安装phalcon,将phalcon的扩展php_phalcon.dll添加到PHP的ext目录下,这个不做赘述,网上教程很多 下面直接安装phalcon-devtools, 1,分别下载phalc ...

  5. LeetCode Array Easy121. Best Time to Buy and Sell Stock

    Description Say you have an array for which the ith element is the price of a given stock on day i. ...

  6. JQ实现仿淘宝条件筛选

    首先看下效果: Js代码: <script type="text/javascript"> $(".search_qxxx > ul > li & ...

  7. Gym 102028J 扫描线/二维差分 + 解方程

    题意:有一个二维平面,以及n个操作,每个操作会选择一个矩形,使得这个二维平面的一部分被覆盖.现在你可以取消其中的2个操作,问最少有多少块地方会被覆盖? 思路:官方题解简洁明了,就不细说了:https: ...

  8. java中文件下载的思路(参考:孤傲苍狼)

    文件下载 文件下载功能是web开发中经常使用到的功能,使用HttpServletResponse对象就可以实现文件的下载 文件下载功能的实现思路: 1.获取要下载的文件的绝对路径 2.获取要下载的文件 ...

  9. 思维+multiset优化——cf1249E

    正着想很难,但是反着想就容易有思路 /* 将问题转化为 挑选最多的线段,每个点的覆盖次数不超过k次 multiset里存k个右端点,表示第i层当前的最远右端点,每次来一根新线段,能填就填进并更新,不能 ...

  10. 暴力枚举+扫描线+线段树——cf1194E

    /*思路就是枚举矩形下面那条先,把所有和其交叉的竖线更新进线段树,然后扫描先向上更新,遇到竖线上端点就在线段树里删掉,遇到横线就更新答案*/#include<bits/stdc++.h> ...