三.多线程传递参数
<span style="font-size:24px;">#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
#include <time.h>
void run(void *p) {
int *px = p;
char str[100] = { 0 };
sprintf(str, "锄禾日当午%d", *px);
MessageBoxA(0, str, "天朝很痛苦", 0);
}
void main1() {
int a[10] = { 1,2,3,4,5,6,7,8,9,10 };
for (int i = 0; i < 10; i++) {
HANDLE hd=_beginthread(run, 0,&i);
//WaitForSingleObject(hd, INFINITE);
}
system("pause");
}</span>
1.多线程查找(多个查找、单个查找)
<span style="font-size:24px;">int isfind = 0;
struct findinfo {
int *pstart;//首地址
int length;
int findnum;
int id;//编号
};
void findit(void *p) {
struct findinfo *ps = p;//保存地址
printf("线程%d开始查找", ps->id);
//遍历首地址,长度为10个元素
for (int *pf = ps->pstart; pf < ps->pstart + ps->length; pf++) {
if (isfind == 1) {
printf("\n线程%d结束查找,其他线程已经找到", ps->id);
return;
}
if (*pf == ps->findnum) {
printf("线程%d结束查找,找到数据%d地址%p\n", ps->id, *pf, pf);
isfind = 1;
//return;
return;
}
}
printf("线程%d结束查找,没有找到\n", ps->id);
}
void main() {
int a[100] = { 0 };
time_t ts;
unsigned int data = time(&ts);
srand(data);
for (int i = 0; i < 100; i++) {
a[i] = rand() % 100;
printf("%4d", a[i]);
if ((i+1) % 10 == 0) {
printf("\n");
}
}
int num = 0;
scanf("%d", &num);
struct findinfo info[10];//结构体数组,保存每个数组要查找的信息
for (int i = 0; i < 10; i++) {
info[i].pstart = a + 10 * i;//首地址
info[i].length = 10;
info[i].id = i;
info[i].findnum = num;
HANDLE hd=_beginthread(findit, 0, &info[i]);
//WaitForSingleObject(hd, INFINITE);
}
system("pause");
}</span>
2.多线程切割
<span style="font-size:24px;">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <Windows.h>
int isfind = 0;
struct findinfo {
int *pstart;
int length;
int findnum;
int id;//编号
};
void findit(void *p) {
struct findinfo *ps = p;//保存地址
printf("\n线程%d开始查找", ps->id);
//遍历首地址,长度10个元素
for (int *pf = ps->pstart; pf < ps->pstart + ps->length; pf++)
{
if (isfind == 1)
{
printf("\n线程%d结束查找,其他线程已经找到", ps->id);
return;
}
if (*pf == ps->findnum)//相等
{
printf("结束查找找到数据%d地址%p", ps->id, *pf, pf);
isfind = 1;
return;
}
Sleep(500);
}
printf("\n线程%d结束查找,没有找到", ps->id);
}
#define M 100 //数据
#define N 8 //线程数量
void main() {
int a[M] = { 0 };
time_t ts;
unsigned int data = time(&ts);
srand(data);
for (int i = 0; i < 100; i++) {
a[i] = rand()%100;
printf("%4d", a[i]);
if ((i + 1) % 10 == 0) {
printf("\n");
}
}
int num;
scanf("%d", &num);
struct findinfo info[N];
if (M%N == 0) {
for (int i = 0; i < N; i++) {
info[i].pstart = a + M/N * i;
info[i].length = M/N;
info[i].id = i;
info[i].findnum = num;
HANDLE hd = _beginthread(findit, 0, &info[i]);
}
}
else {
for (int i = 0; i < N-1; i++) {
info[i].pstart = a + M / (N-1) * i;
info[i].length = M / (N-1);
info[i].id = i;
info[i].findnum = num;
HANDLE hd = _beginthread(findit, 0, &info[i]);
}
//info[N-1]
int i = N - 1;
info[i].pstart = a + M / (N - 1) * i;
info[i].length = M % (N - 1);
info[i].id = i;
info[i].findnum = num;
HANDLE hd = _beginthread(findit, 0, &info[i]);
}
system("pause");
}</span>
七.指针数组强化
1.数组逆置
void rev(int *p, int n)//长度
{
for (int i = 0; i < n / 2;i++)
{
int temp = p[i];
p[i] = p[n - 1 - i];
p[n - 1 - i] = temp;
}
}
void revp(int *p, int n)//长度
{
for (int *phead = p, *pback = p + n - 1; phead < pback;phead++,pback--)
{
int temp = *phead;
*phead = *pback;
*pback = temp;
}
}
void show(int *p, int n)
{
for (int i = 0; i < n;i++)
{
printf("%4d", p[i]);
}
}
void main4()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//10/2 《5 //0-4
int b[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};//9/2 0-3 4
revp(a, sizeof(a) / sizeof(a[0]));
show(a, sizeof(a) / sizeof(a[0]));
putchar('\n');
revp(b, sizeof(b) / sizeof(b[0]));
show(b, sizeof(b) / sizeof(b[0]));
system("pause");
}
八.队列与多线程
1.struct的两种取值
struct Myinfo info;
方法一:info.length=100;
方法二:(&info)->length=100;
(如果用箭头赋值必须是地址)
2.多线程结构体应用
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
#include <time.h>
#define N 1024
struct Myinfo {
int *pstart;//开始地址
int id;
int length;
int sum;
};
void add(void *p) {//void可以保存任何类型的指针,也可以转化为任何类型的指针
struct Myinfo *pinfo = p;
for (int i = 0; i < pinfo->length; i++) {
pinfo->sum += pinfo->pstart[i];
}
printf("线程%d的结果%d\n", pinfo->id, pinfo->sum);
}
void main() {
time_t ts;
unsigned int data = time(&ts);
srand(data);
int num[N] = { 0 };
for (int i = 0; i < N; i++) {
//num[i] = rand() % 1000;
printf("%4d", num[i] = rand() % 1000);
if ((i + 1) % 18 == 0) {
printf("\n");
}
}
printf("\n");
struct Myinfo info[8] = { 0 };
for (int i = 0; i < 8; i++) {
info[i].id = i;
info[i].length = N / 8;
info[i].sum = 0;
info[i].pstart = num + i*N / 8;
_beginthread(add, 0, &info[i]);
}
system("pause");
int lastsum = 0;
for (int i = 0; i < 8; i++) {
lastsum += info[i].sum;
}
printf("\n多线程总和=%d", lastsum);
system("pause");
}