C语言实现并发查找

#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] = 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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值