多线程查找

来源:互联网 发布:一部电脑一个网络端口 编辑:程序博客网 时间:2024/05/19 10:37
#include<stdio.h>#include<stdlib.h>#include<process.h>#include<Windows.h>int  flag = 0;//意味着没有找到int  * addrfind = NULL;//地址struct MyStruct{int *pfindstart;//要查找的首地址int  length;//限定长度,从地址开始int  num;//要查找的数据int  bh;//编号int  *pflag;//传递flag的地址,修改flag,访问int **addr;//传递一个指针的地址,};void find(void *p){struct MyStruct *pstruct = (struct MyStruct *)p;//指针类型转换//内存的遍历,从地址开始累加100个元素的大小,遍历所有元素for (int *px = pstruct->pfindstart; px < pstruct->pfindstart + 100; px++){Sleep(100);if (*(pstruct->pflag) != 0){printf("属下%d无能,其他线程已经找到", pstruct->bh);return;}if (*px == pstruct->num)//判断是否相等{printf("\n第%d个线程找到%p,%d",pstruct->bh, px,*px);//查找*(pstruct->pflag) = 1;//改变标识,代表找到*(pstruct->addr) = px;return;}}printf("\n没有找到第%d个线程", pstruct->bh);return;}void main(){int a[1000];//783;数组for (int i = 0; i < 1000; i++){a[i] = i;//数组初始化}struct MyStruct threaddata[10];//创建结构体,处理不同的线程,for (int i = 0; i < 10; i++)//创建10个线程并行查找{//a , a+100 a+200               a+900threaddata[i].pfindstart  = a + i * 100;//计算数组的地址threaddata[i].length = 100;//长度threaddata[i].bh = i;//编号threaddata[i].num = 783;//要查找的数据threaddata[i].pflag = &flag;//标识threaddata[i].addr = &addrfind;//存储了变量的地址printf("\n%d\n", threaddata[i].bh);_beginthread(find, 0, &threaddata[i]);////Sleep(30);///}Sleep(30000);system("pause");printf("\n\n%d,%p\n\n", *addrfind, addrfind);//打印地址,还有数据system("pause");}

原创粉丝点击