github源码指引:共享内存、数据结构与算法:作为基础的数组

初级代码游戏的专栏介绍与文章目录-CSDN博客

我的github:codetoys,所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。

这些代码大部分以Linux为目标但部分代码是纯C++的,可以在任何平台上使用。


        相关专题:共享内存、数据结构与算法_初级代码游戏的博客-CSDN博客 

        源码位置:shmfc

目录

一、运行示例

二、示例代码


一、运行示例

        shmfc目录下执行run.sh,显示命令表。系统重启后初次运行需要先执行命令0-1创建主共享内存(执行完毕输入b返回上一级命令表)。

        命令20执行本示例代码。

[08-27 08:44:24][应用][信息][shmfc_t.cpp             : 851(main)][  0.00]
----------------------------------------
命令表:(q=exit)
0 管理
1 test_BinaryPool
3 test_CMultiProcessServer
4 test_CMultiProcessServer_view
5 test_CMultiProcessServer_speed
6 test_CMultiProcessServer_direct
7 test_RebuildSet
12 UDP测试客户端
13 UDP测试服务端
14 CStressTesting
15 CTestCSimpleMultiProcess_mutex atomic
16 CTest_hash
17 T_SHM_HASH
20 T_ARRAY
88 test_CMyRWMutex
89 test_T_SHM_SET_GROUP
90 test_shm_IActiveObject
91 test_ShmMultiMap
99 test_ParseFromXml
........................................
----------------------------------------
请选择命令:(q=exit default=20):

[08-27 08:44:30][应用][信息] 用户输入的是:20

        运行输出:

[08-27 08:44:30][应用][信息][shmArray.h              : 960(AttachToShm)][  0.00]test 连接共享内存成功 PI_N 1 PART 0 shmid = 32769 p 0x7ff9eaabc000
[08-27 08:44:30][应用][信息][shmEnv.cpp              : 624(GetShmConfig)][  0.00]未配置 SHM_CONFIG default.test
[08-27 08:44:30][应用][出错][shmEnv.h                : 304(GetRegFromDb)][  0.00]共享内存 default test 0 不存在,可能的原因:主机重启或手工删除
[08-27 08:44:30][应用][信息][shmArray.h              :1024(_DestoryShm)][  0.00]test 0 连接到共享内存失败
[08-27 08:44:30][应用][信息][shmEnv.cpp              : 624(GetShmConfig)][  0.00]未配置 SHM_CONFIG default.test
[08-27 08:44:30][应用][信息][shmArray.h              :1074(_CreateShm)][  0.00]default test 0 未配置的最大记录数
[08-27 08:44:30][应用][信息][shmArray.h              :1076(_CreateShm)][  0.00]使用默认值 1024
[08-27 08:44:30][应用][信息][shmArray.h              : 395(_CreateShmIfNeed)][  0.00]创建新共享内存成功,id = 65537
[08-27 08:44:30][应用][信息][shmArray.h              :1129(_CreateShm)][  0.00]基础共享内存创建完成
[08-27 08:44:30][应用][信息][shmArray.h              : 960(AttachToShm)][  0.00]test 连接共享内存成功 PI_N 1 PART 0 shmid = 65537 p 0x7ff9eaabc000
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]0
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]1
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]2
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]3
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]4
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]5
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]6
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]7
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]8
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.00]9
。。。。。。
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]93
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]94
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]95
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]96
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]97
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]98
[08-27 08:44:30][应用][信息][shmfctest2.h            :  68(test_T_ARRAY)][  0.01]99
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第0个数据:10000 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第1个数据:10001 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第2个数据:10002 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第3个数据:10003 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第4个数据:10004 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第5个数据:10005 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第6个数据:10006 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第7个数据:10007 abc
。。。。。。
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第97个数据:10097 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第98个数据:10098 abc
[08-27 08:44:30][应用][信息][shmfctest2.h            :  73(test_T_ARRAY)][  0.01]第99个数据:10099 abc
[08-27 08:44:30][应用][信息][shmIActiveObject.h      : 457(RunCmdUI)][  0.01]
test

b:返回上一层
1:创建 2:连接(只读) 3:连接(可写) 4:断开 5:禁用互斥 6:清除数据 7:创建私有 8:删除共享内存 9:显示 10:数据
11:从数据库加载 12:保存到数据库
21:从目录加载 22:保存到目录 23:导出为文本文件
31:从目录加载到私有内存 32:销毁私有内存
97:repair 98:check 99:ToDo 100:shell(q=exit ):

        测试最后进入本块共享内存的管理界面,直接回车显示基本信息:

[08-27 08:52:37][应用][信息] 用户输入的是:
[08-27 08:52:37][应用][信息][shmIActiveObject.h      : 467(RunCmdUI)][  0.01]
test
SHM NAME PI CSW TOTAL BLOCK R_LENGTH CAPACITY CAPACITY SIZE SIZE    % OPERATION DUAL RESULT OPERATION DUAL RESULT
-------- -- --- ----- ----- -------- -------- -------- ---- ---- ---- --------- ---- ------ --------- ---- ------
test      1 CSW 18.7K     1       16     1024     1.0K  100 100  9.0%
-------- -- --- ----- ----- -------- -------- -------- ---- ---- ---- --------- ---- ------ --------- ---- ------


        test是共享内存的名字,代码里设定的,全系统唯一。

        PI是内部使用的索引,进程内唯一。

        CSW,C表示已连接,S表示是共享内存(而不是私有内存),W表示可写。

        BLOCK表示分块数,超出容量会自动增加一块连接在一起(虚拟)。

        TOTAL是总字节数,R_LENGTH是记录字节数,容量和大小都是记录数(各两个,精确值和简明值)。

二、示例代码

        示例代码:

//
// Copyright (c) ct  All rights reserved.
// 版权所有 ct 保留所有权利
//

#pragma once


#include <sstream>
#include "../env/env.h"
#include "shmArray.h"

class CTestT_ARRAY
{
public:
	struct DemoData : public CActiveObjectBase
	{
		long n = 0;
		sstring<8> s;

		//用于需要排序的场合
		bool operator < (CDemoData const& tmp)const { return n < tmp.n; }
		//某些场合也需要等于
		bool operator == (CDemoData const& tmp)const { return n == tmp.n; }

		friend ostream& operator << (ostream& o, DemoData const& d)
		{
			return o << d.n << " " << d.s.c_str();
		}
		//关键字的hash值,用于分块场合,应保证hash值的最后一部分仍然是平均分布的
		long keyhash()const { return n; }

		//用于输出数据的场合
		string& toString(string& str)const
		{
			char buf[2048];
			sprintf(buf, "%ld %s", n, s.c_str());
			return str = buf;
		}
		//用于表格输出
		static bool AddTableColumns(CHtmlDoc::CHtmlTable2& table)
		{
			table.AddCol("N", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			table.AddCol("S", CHtmlDoc::CHtmlDoc_DATACLASS_RIGHT);
			return true;
		}
		bool AddTableData(CHtmlDoc::CHtmlTable2& table)const
		{
			table.AddData(n);
			table.AddData(s.c_str());
			return true;
		}
	};

	static int test_T_ARRAY(int argc, char** argv)
	{
		T_ARRAY<DemoData, PI_TEST_1, CDemoData > a("test", 1);
		a.DestoryShm();
		if (!a.CreateShm())return __LINE__;
		if (!a.Attach(false))return __LINE__;
		for (int i = 0; i < 100; ++i)
		{
			DemoData tmp;
			tmp.n = 10000 + i;
			tmp.s = "abc";
			T_ARRAY<DemoData, PI_TEST_1, CDemoData >::HANDLE h;
			a.Add(tmp, h);
			thelog << h.handle << endi;
		}
		for (int i = 0; i < a.Size(); ++i)
		{
			string str;
			thelog << "第" << i << "个数据:" << a.Get(i)->toString(str) << endi;
		}
		a.RunCmdUI();

		return 0;
	}
};

        代码不复杂,先定义了记录结构,然后声明了一个共享内存类型。


(这里是文档结束)

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值