C语言实现并发查找
2016-03-19 01:09
344 查看
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
int find = 0;
int *pfind = NULL;
struct Mystruct
{
int *init;
int length;
int initdestination;
int num;
};
void getDestination(void *p);
int main()
{
int a[10000];
for (int n = 0;n < 10000;n++)
{
a
= n;
}
struct Mystruct Data[10];
for (int i = 0;i < 10;i++)
{
Data[i].init = a + i * 1000;
Data[i].length = 1000;
Data[i].initdestination = 8564;
Data[i].num = i;
HANDLE hd = (void *)_beginthread(getDestination, 0, &Data[i]);
WaitForSingleObject(hd, INFINITE);
}
Sleep(10000);
if(pfind == NULL)
{
*pfind = 0;
}
printf("\n地址为:%p\n值为:%d\n", pfind, *pfind);
system("pause");
}
void getDestination(void *p)
{
struct Mystruct *sp = (struct Mystruct *)p;
for (int *initp = sp->init; initp < sp->init+sp->length; initp++)
{
Sleep(1);
if(find !=0)
{
printf("无力,第%d个线程没有找到\n",sp->num);
return;
}
if(*initp == sp->initdestination)
{
find = 1;
pfind = initp;
printf("找到了,第%d个线程\n",sp->num);
return;
}
}
printf("没有找到,第%d个进程\n", sp->num);
}
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
int find = 0;
int *pfind = NULL;
struct Mystruct
{
int *init;
int length;
int initdestination;
int num;
};
void getDestination(void *p);
int main()
{
int a[10000];
for (int n = 0;n < 10000;n++)
{
a
= n;
}
struct Mystruct Data[10];
for (int i = 0;i < 10;i++)
{
Data[i].init = a + i * 1000;
Data[i].length = 1000;
Data[i].initdestination = 8564;
Data[i].num = i;
HANDLE hd = (void *)_beginthread(getDestination, 0, &Data[i]);
WaitForSingleObject(hd, INFINITE);
}
Sleep(10000);
if(pfind == NULL)
{
*pfind = 0;
}
printf("\n地址为:%p\n值为:%d\n", pfind, *pfind);
system("pause");
}
void getDestination(void *p)
{
struct Mystruct *sp = (struct Mystruct *)p;
for (int *initp = sp->init; initp < sp->init+sp->length; initp++)
{
Sleep(1);
if(find !=0)
{
printf("无力,第%d个线程没有找到\n",sp->num);
return;
}
if(*initp == sp->initdestination)
{
find = 1;
pfind = initp;
printf("找到了,第%d个线程\n",sp->num);
return;
}
}
printf("没有找到,第%d个进程\n", sp->num);
}
相关文章推荐
- 使用 Syncthing 在多个设备间同步文件
- C#实现多线程的同步方法实例分析
- ruby实现的一个异步文件下载HttpServer实例
- C#异步绑定数据实现方法
- Redis中实现查找某个值的范围
- 科学知识:同步、异步、阻塞和非阻塞区别
- 同步文件备份工具 Super Flexible File Synchronizer Pro v4
- 探讨Ajax中同步与异步之间的区别
- 批量文件查找替换功能的vbs脚本
- C#线程同步的三类情景分析
- WinForm实现按名称递归查找控件的方法
- 一个用vbs查找硬盘所有分区中的指定程序的代码
- C#中异步回调函数用法实例
- 浅析Node.js查找字符串功能
- C++使用CriticalSection实现线程同步实例
- C++二分法在数组中查找关键字的方法
- C++中字符串查找操作的两则实例分享
- 探究在C++程序并发时保护共享数据的问题
- 异步编程需要“意识” 推荐