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

  一般 多线程传递参数 为 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

/*程序流程为:主线程创建子线程(当前子线程状态为stop停止状态),5秒后主线程唤醒子线程,10秒后主线程挂起子线程,15秒后主线程再次唤醒子线程,20秒后主线程执行完毕等待子线程退出。

代码如下:*/
#include "stdio.h"
#include "unistd.h"
#include "pthread.h"
#include "string.h"
#include "time.h" #define RUN 1
#define STOP 0 pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER; int status = STOP;
void * thread_function(void)
{
static int i = 0;
while (1)
{
pthread_mutex_lock(&mut);
while (!status)
{
pthread_cond_wait(&cond, &mut);
}
pthread_mutex_unlock(&mut); printf("child pthread %d\n", i++);
if (i == 20)
break;
sleep(1);
}
} void thread_resume()
{
if (status == STOP)
{
pthread_mutex_lock(&mut);
status = RUN;
pthread_cond_signal(&cond);
printf("pthread run!\n");
pthread_mutex_unlock(&mut);
}
else
{
printf("pthread run already\n");
}
} void thread_pause()
{
if (status == RUN)
{
pthread_mutex_lock(&mut);
status = STOP;
printf("thread stop!\n");
pthread_mutex_unlock(&mut);
}
else
{
printf("pthread pause already\n");
}
} int main()
{
int err;
static int i = 0;
pthread_t child_thread; #if 0
if (pthread_mutex_init(&mut, NULL) != 0)
printf("mutex init error\n");
if (pthread_cond_init(&cond, NULL) != 0)
printf("cond init error\n");
#endif err = pthread_create(&child_thread, NULL, (void *)thread_function, NULL);
if (err != 0 )
printf("can't create thread: %s\n", strerror(err));
while(1)
{
printf("father pthread %d\n", i++);
sleep(1);
if (i == 5)
thread_resume();
if (i == 10)
thread_pause();
if (i == 15)
thread_resume();
if (i == 20)
break;
}
if (0 == pthread_join(child_thread, NULL))
printf("child thread is over\n");
return 0;
}

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

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

Linux 。。。   pthread_join()

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

linux 退出线程  pthread_exit()

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

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

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

#include "stdio.h"
#include "omp.h"
#include "time.h"
#include "unistd.h"
#include "pthread.h" clock_t start,end; void* test(void *p){ start = clock();
int i;
for(i=0;i<100000;i++)
usleep(1); end = clock(); printf("process test %d\n",end-start);
return ((void *)0);
} void* test1(void *P){
start = clock();
int i;
#pragma omp parallel for
for(i = 0;i<100000;i++)
usleep(1); end = clock();
printf("process test1 %d\n",end-start);
return ((void *)0); }
int main(){ int err; pthread_t ntid;
pthread_t ntid1;
void** out; err = pthread_create(&ntid,0,test,0); if(err !=0) putchar('N');
err = pthread_create(&ntid1,0,test1,0); if(err !=0) putchar('N');
// test(0);
// test1(0); printf("Main process\n");
pthread_join(ntid,out);
pthread_join(ntid1,out); return 0; }

  

windows :

#include <windows.h>
#include <iostream>
#include <process.h>
#include <time.h> #define _CRT_SECURE_NO_WARNINGS
using namespace std; CRITICAL_SECTION cs; int i = 0; void run(void *){ char num[30];
while (1){
sprintf(num,"title %d",i++);
system(num);
Sleep(1000);
//MessageBox(0,(LPCTSTR)num,(LPCTSTR) num, 0);
} } int main(){ int hd[4];
MessageBoxA(0, "1", "1", 0);
// for (int i = 0; i < 4; i++){
hd[i] = _beginthread(run, 0, 0);
// }
WaitForSingleObject(hd, true);
system("pause");
return 0;
}

  

参考文献:

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. 第九章 Service

    2019-09-23 今天距离2020年刚好有一百天,希望在未来的百日里能不负期待 不忘初心,方得始终, 初心易得,始终难守. 一.Service 的概念 Kubernetes Service定义了这 ...

  2. 项目案例之Pipeline流水线发布JAVA项目(三)

    项目案例之Pipeline流水线发布JAVA项目(三) 链接:https://pan.baidu.com/s/1NZZbocZuNwtQS0eGkkglXQ 提取码:z7gj 复制这段内容后打开百度网 ...

  3. 在Kubernetes下部署Prometheus

    使用ConfigMaps管理应用配置 当使用Deployment管理和部署应用程序时,用户可以方便了对应用进行扩容或者缩容,从而产生多个Pod实例.为了 能够统一管理这些Pod的配置信息,在Kuber ...

  4. Python文件路径操作

    print(os.environ.get('HOME')) # 打印`HOME`这个环境变量 /Users/<> file_path = os.environ.get('HOME') + ...

  5. Vue学习笔记【23】——Vue组件(组件的定义)

    定义Vue组件 什么是组件: 组件的出现,就是为了拆分Vue实例的代码量的,能够让我们以不同的组件,来划分不同的功能模块,将来我们需要什么样的功能,就可以去调用对应的组件即可: 组件化和模块化的不同: ...

  6. 【文件分层】/var/run

    /var/run是干什么用的 根据linux的文件系统分层结构标准(FHS)中的定义: /var/run 目录中存放的是自系统启动以来描述系统信息的文件. 比较常见的用途是daemon进程将自己的pi ...

  7. [20190725NOIP模拟测试8]题解

    Orz T1 大水题,考场上看到题目中什么前几位相同末尾加字母莫名慌的一批 后来发现直接无脑哈希就能$O(n)$ KMP同样可切 仔细读题,数组别开小 #include<cstdio> # ...

  8. this关键字的使用!

    class Student{ String name; int age; Student(String name,int age){ this.name=name; this.age=age; } S ...

  9. STM32嵌入式开发学习笔记(三):使用按键控制小灯

    按键和小灯一样,也是通过GPIO外设与主板连接,也是通过GPIO_InitStruct类型结构体控制其工作. 查阅技术手册,按钮连接GPIOA控制下的管脚0. 但与之不同的是,按键是一种输入设备,输入 ...

  10. P1493 分梨子

    P1493 分梨子 题目描述 Finley家的院子里有棵梨树,最近收获了许多梨子.于是,Finley决定挑出一些梨子,分给幼稚园的宝宝们.可是梨子大小味道都不太一样,一定要尽量挑选那些差不多的梨子分给 ...