注意,结构体传入数据
一开始int* 不能变成void
传入int ** 也会失效,
只能一开始在message 结构体里有 int* 类型才能运行
这是通过打印表格得到的
使用raylib 开发
#include <raylib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <pthread.h>
typedef struct message {
int flag;
int a;
int user;
void* work;
int* map;
} message;
void* showv2(void* msg) {
static int cnt = 0;
static int color = 1;
message* m = (message*)msg;
// 二维数组失效
// int** meshmap = (int**)m->work;
// 一维数组测试
// int* map =(int*)(*m).work;
int* map = m->map;
while (1) {
while (m->flag == 0 || m->a == 1) {
// _sleep(1);
_sleep(0);
printf("B 发现占用中 %d %d %d %d %d\n", m->a, m->user, m->flag, cnt, color);
}
m->a = 1;
m->user = 1;
printf("B 占用中\n");
for (int p = 0; p < 1500; p++) {
cnt += 1;
if (cnt >= 500 * 500) {
cnt = 0;
color += 1;
}
map[cnt] = color;
// printf("%d\n", cnt);
}
// printf("%d %d\n", cnt, color);
printf("B 释放占用\n");
m->a = 0;
_sleep(30);
}
}
int main() {
InitWindow(500, 500, "okk");
RenderTexture mesh = LoadRenderTexture(500, 500);
BeginTextureMode(mesh);
ClearBackground(WHITE);
EndTextureMode();
message msg;
msg.a = 0;
msg.flag = 0;
msg.user = -1;
int*map = new int[500 * 500];
for (int i = 0; i < 500 * 500; i++) {
map[i] = 0;
}
msg.work = (void*)↦
msg.map = map;
pthread_t p;
pthread_create(&p, NULL, showv2, (void*)&msg);
int i = 0;
int j = 0;
int cnt = 0;
int color = 1;
int pv2 = 0;
SetTargetFPS(60);
while (1) {
while (msg.a == 1) {
printf("C 发现占用中%d\n", msg.user);
_sleep(10);
}
msg.a = 1;
msg.user = 999;
printf("C 占用中\n");
BeginTextureMode(mesh);
for(int i=0;i<500*500;i++){
DrawPixel(i%500,i/500,{map[i]*10%255,map[i]*50%255,map[i]*30%255,255});
}
EndTextureMode();
msg.a = 0;
printf("C 释放\n");
BeginDrawing();
DrawTexturePro(mesh.texture,{0,0,500,500},{0,0,500,500},{0,0},0,WHITE);
DrawTexture(mesh.texture, 0, 0, WHITE);
DrawText(TextFormat("fps %d", GetFPS()), 20, 30, 50, {155, 50, 210, 255});
EndDrawing();
static int flag = 0;
if (flag > 30) {
flag = 300;
msg.flag = 1;
} else {
flag++;
}
}
}