您的位置:首页 > 编程语言 > C语言/C++

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);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  并发 查找 同步 异步