多线程 + 网络 + 概率 + 基础 + 文件

来源:互联网 发布:java版qq是什么意思 编辑:程序博客网 时间:2024/05/19 17:57

cocos2dx多线程以及线程同步 与 cocos2dx内存管理与多线程问题 ------- 火车售票      

iOS开发Swift篇(02) NSThread线程相关简单说明 --- http://www.cnblogs.com/wendingding/p/5409149.html

 

C++11 多线程  -------  http://www.cnblogs.com/zhuyp1015/archive/2012/04/08/2438288.html

c++11 多线程  -- 基本使用--http://www.2cto.com/kf/201507/419768.html

C++11 并发指南三(std::mutex 详解)

new && new(std::nothrow)   ---   http://blog.sina.com.cn/s/blog_65d204330101do6r.html


 C++类型转换函数   ---- C语言中文网  http://c.biancheng.net/cpp/biancheng/view/222.html    .

 函数调用 堆栈          ---  http://blog.csdn.net/zhanglianpin/article/details/6310873

 多重继承中二义性   --- http://blog.csdn.net/zhangyishuihan/article/details/7233914

google protobuf特点及语法   ---  http://wanglimin2004.blog.163.com/blog/static/115488498201372221518780/

-----------------------------------------------------------------------------------------------------------  2017年1.16日

运行效率


运算符重载:  隐含了第一个参数this指针


返回引用类型:


--------------------------------------------------------------------------------------------------------------------网络:2017年1.10日

1   心跳包:http://blog.csdn.net/xuyuefei1988/article/details/8279812

 实例:框架以前PC都是长连接   后来手机改为短连接。 

2

短连接:数据发送完就断开, 长连接:等结束才断开。 http短连接<完后断开connect>,socket既:可长连又能短连. 


问题来了:长连接时服务器怎么知道client是否在线那?
心跳包:用于判断是否掉线,Logic层实现的, client用Timer定时发一个数据包<heartBeat>,服务器Timer定时检测如果在规定时间没收到,视为断网(单独线程)


TCp:TCP的机制里面,本身是存在有心跳包的机制的,why不用,因为系统默认设置2小时的心跳频率。so2h太长... 

http://blog.csdn.net/u013321328/article/details/44887009 -- 自己CSDN

 3

进程间通信的方式有 共享内存, 管道 ,Socket ,消息队列等。
郑亮:进程通信常见有socket 。server在主线程发infor,子线程收消息。 Or会阻塞。 多线程有很多问题:如争夺资源

 --------------------------------------------------------------------------------------------------------------------文件:2016年12月

 文件操作:Log不能覆盖旧的              FILE *fp = fopen(fileName.c_str(), "a+");

 //http://wenda.so.com/q/1462487621725072 -  http://wenda.so.com/q/1364838871065534?src=140 --   http://www.cnblogs.com/wangzijing/archive/2013/03/02/2940466.html

--------------------------------------------徐国安       2017.3.4         

1  char: 8bit= 1byte  字节流/二进制   http://blog.csdn.net/moxiaomomo/article/details/6647939

2 序列化:顺序一样  名字无所谓是。因为server是根据sizeof来判断的

 ------------------------------------------- 框架:简单方法解析字符串

GetMonthDays //获取某年中的某个月有多少天
IsLeapYear             //是否闰年

IsValidIDCardNumber//验证身份证号码是否正确


读取配置文件不外呼: json   xml   自定义<.ini .cfg>

*------------------------------------------------------------------------------------动画

void UITaskDaily::FoundItem(Node* pAddToNode)
{
m_pTaskNode = CSLoader::createNode("CSD/TaskDaily.csb");
pAddToNode->addChild(m_pTaskNode);
m_pTaskNode->setVisible(false);
m_pTaskAction = CSLoader::createTimeline("CSD/TaskDaily.csb");
m_pTaskNode->runAction(m_pTaskAction);
}
void UITaskDaily::onShow()
{
m_pTaskAction->gotoFrameAndPlay(0, 15, false);
m_pTaskAction->setLastFrameCallFunc(CC_CALLBACK_0(UITaskDaily::onShowEnd, this));
}

void UITaskDaily::onShowEnd()
{
m_pTaskNode->setVisible(true);
m_pTaskAction->clearLastFrameCallFunc();
}

----- http://blog.csdn.net/fansongy/article/details/12955989     Cocos2d-x 3.0 开发(七)在程序中处理cocoStudio导出动画


--------------------------------线程与进程
线程与进程的区别: 最小调度单位   共享资源

进程与进程间并发执行。 进程内部线程:也是并发执行
多线程编程
      线程间的互斥与同步:物理设备/  shunxu
           临界资源和临界区:  它们要求被互斥的访问
           互斥量(Mutex) (全局的互斥量)

--------------------------------ios

  

iOS开发零基础教程之开发证书以及打包问题  http://www.myexception.cn/iphone/302252.html

--------------------郭新泉:

去年进步最大。    杨辉 秋 程勇都换工作了。   不能没能力 形成依赖 为自己工作


----------------高报 基础

1  线程有主子之分,进程无就一个  2:muntex互斥量不能解决所有的,4种都得了解  3:muntex就是解决线程通信问题 4:sleep经常用,不使用浪费CPU资源  

2  生产者 消费者


做视频直播:实质一个sprite不断修改texture。而纹理数据是从server获取的

 B 多线成共享资源---so变量必须是全局 ---- 互斥锁(全局变量)


 

  _thread = new std::thread(&UIGameAnnouncements::ReadXML, this);
 //_thread->join();   //为啥不会走了!阻塞主线程。  啥时候使用?

 //1 线程开 关闭。(资源释放时join调用)  2比如多个线程都访问一个变量,  --- 成高爆

  _thread2 = new std::thread(&UIGameAnnouncements::helloWorld, this);
 
 CCLOG("----------ove------");
}        


void   UIGameAnnouncements::helloWorld()
{
   while (true)
   {
 //   _muntex.lock();
    if (num > 0)
    {
     CCLOG("----------helloWorld------: %d", num);
     num--;
  //   _muntex.unlock();
    }
    else
    {
 //    _muntex.unlock();
     break;
    }
    std::this_thread::sleep_for(chrono::seconds(1));
   }
}

void  UIGameAnnouncements::ReadXML()
{
 while (true)
 {
 // _muntex.lock();
  if (num > 0)
  {
   CCLOG("-------------MainThread-----ReadXML----: %d", num);
   num--;
 ///  _muntex.unlock();
  }
  else
  {
 //  _muntex.unlock();
   break;
  }
  std::this_thread::sleep_for(chrono::seconds(1));   //不能省去  or出不来 同一张票卖了2次
 }
}

服务器代码调试   ---------  http://snoopyxdy.blog.163.com/blog/static/60117440201404105333383/

头说: 桌子服务器 是需要才创建

 

--------------------------------------------------我不会-------概率: 问了后采弄明白

----------要求:某张牌按一定概率出现
1   鱼,财神,招财猫出现几率: 按概率和比牌张数
比一张:0.97      0.2      0.1  (和=100%)
二:       0.96,  0.03    0.01
三:       0.80      0.15    0.5

注:石头 剪刀  布 随机

 

2 随机数使用
 rand()%N :生成0-99之间整数
srand(time(NULL)) 使用前要设置种子, 通常赋值为time(NULL)

-----------------------------上面代码:

#define ISRANGE(n,a,b)            (((a) > (b)) ? (a>n&&n>=b) : (a<=n&&b>n))

ardType CGameLogic::GetACardType()
{
 SetProOfEmergence(g_SystemParam.fProOfEmergence);
 float fPro[3]= {0};
 switch(m_nCardCount)
 {
 case 1:
  memcpy(fPro, m_nProOfEmergence[0],sizeof(fPro));     //比一张牌时, 不同牌型出现概率(0.97  0.2    0.1  (和=100%))
  break;
 case 3:
  memcpy(fPro, m_nProOfEmergence[1],sizeof(fPro)); // 3 (0.96,   0.03   0.01)
  break;
 case 5:
  memcpy(fPro, m_nProOfEmergence[2],sizeof(fPro));  // 5(0.80     0.15   0.5)
  break;
 }


 int nStartNum = 0;
 srand(time(nullptr));
 int tt_rand = rand()%100;   (总和100%)
 for (int n = 0;n< 3 ;n++)
 {
  if (ISRANGE(tt_rand,nStartNum,nStartNum+(fPro[n]*100)))        // 判断随机数是否在区间/某个概率范围内
  {
   return (CardType)(n+1);
  }
  nStartNum+=(fPro[n]*100);
 }
 return ctType1;
}


 -----------------------------------80/20法则: 时间空间 (许国安)

要求: 从一个字符串中提取出来汉字

 

 string _strContent =  "您存<a,b/f >在 幸 运a符/飞";
 string output;
 for (int i = 0; i < _strContent.size(); i++)
 {
  if (_strContent[i] == '<') // 不需要的
  {
  }
  else
  {
    //output.append( _strContent[i])    //开始这样写不对, 直接看源码。。。
    output.append(1, _strContent[i]);  // 这种写法:浪费内存,但是省CPU/时间(因为定义一个变量,  省:每次Del后, 后边元素不用先前移动)
  }
 }

 string _strContent2;
  string    _strTemp2 = _strContent,  _strTemp = _strContent;
 for (int i = _strTemp.size() - 1; i >= 0; i--)
  {
   if (_strTemp[i] >= '0' && _strTemp[i] <= '9')
   {
    _strTemp.erase(_strTemp[i]);   //是这样用的吗? 没差文档/看源码
   }
  }


 string _temp55 = "";
 for (int i=0; i < _strTemp2.size(); i++)
 {
  if (_strTemp2[i] >= '0' && _strTemp2[i] <= '9')
  {
   //continue;
  }
  else
  {
  _temp55 = _temp55 + _strTemp2[i]; //效率很低:A  _temp55 + _strTemp2[i];会产生多个临时对象  B 直接看.erase函数圆形  C到过来遍历,汉字结合错了
  }
 }
 // return 也会产生临时变量。  所以:一般返回引用or指针

------------------------------------------------------------------------------------------------------------文件

使用研发主管读取文件说明:
1  对文件后缀无要求; fopen必须礬2进制方式
2  每行以分号 + Enter结束. ; + \r\n <可以有注释,但不能有=号>
3  定义Map. 把内容放入到Map中再用getValue取出值

+

-----------------------------11.23

bool    UILogin::LoadPeiTileCfg(const string& strFileName)
{
//人数都为固定,且由房间配置
FILE* fp = fopen("MaJaing.ini", "rb");
if (!fp)
return false;


//得到文件大小
fseek(fp, 0, SEEK_END);
long fileLen = ftell(fp);
fseek(fp, 0, SEEK_SET);


//读出所有数据
char* pBuf = new char[fileLen + 1];
fread(pBuf, fileLen, 1, fp);
pBuf[fileLen] = '\0';


fclose(fp);


char* pOld = pBuf;
bool bEnd = false;
/////////分号作为分隔符
int  tt_fenhaoindex = -1; //分号的位置
while (!bEnd)
{
string strLine;
int readNum = 0;
strLine = GetSubSection(pBuf, readNum, bEnd);
pBuf += readNum;
if (strLine.length() > 0)
{
strLine = RemoveSpaceAndN(strLine);
}


if (strLine.length() > 0)
{
ParserSubSection(strLine, false);
}
}


delete[] pOld;
return true;
}


//得到一段,用分号间隔
string    UILogin::GetSubSection(char* pChar, int& readNum, bool& bReadAllEnd)
{
readNum = 0;
bReadAllEnd = false;
string strLine;
char ch[2];
while ((*pChar) != '\0')
{
//换行,分号
if ((*pChar) != '\n' && ((*pChar) != '\r') && ((*pChar) != ';'))
{
ch[0] = *pChar;
ch[1] = '\0';


strLine.append(ch);
pChar++;
readNum++;
}
else
{
pChar++;
readNum++;
break;
}
}


if ((*pChar) == '\0')
{
bReadAllEnd = true;
}


return strLine;
}


//移除空格和换行
string    UILogin::RemoveSpaceAndN(string& strLine)
{
const char* pChLine = strLine.c_str();
size_t len = strLine.length();


char* pCh = new char[len + 1];


int count = 0;


while ((*pChLine) != '\0')
{
if ((*pChLine) != ' ' && ((*pChLine) != '\n') && ((*pChLine) != '\r') && ((*pChLine) != '\t') && ((*pChLine) != ';'))
{
pCh[count] = *pChLine;
count++;
}
pChLine++;
}


pCh[count] = '\0';


string strTemp;
strTemp.append(pCh);
delete[] pCh;


return strTemp;
}


void   UILogin::ParserSubSection(string& strLine, bool bPeiTile)
{
string strKey;
string strValue;
if (IsKeyAndValue(strLine.c_str(), (int)strLine.length(), strKey, strValue))
{
_KeyValueMap.insert(make_pair(strKey, strValue));
}
}


//是否是键和值
bool      UILogin::IsKeyAndValue(const char* pChLine, int charLen, string& strKey, string& strValue)
{
//查找=,进行左右分隔
int dengPos = 0;


int len = charLen;


const char* p = pChLine;


while ((*p) != '\0')
{
if ((*p) == '=')
{
break;
}


p++;
dengPos++;
}


if (dengPos > 0 && dengPos < len)
{
p = pChLine;
char* pChLeft = new char[dengPos + 1];
memcpy(pChLeft, p, dengPos);
pChLeft[dengPos] = '\0';
strKey.append(pChLeft);


delete[] pChLeft;


char* pChRight = new char[len - dengPos];
memcpy(pChRight, (p + (dengPos + 1)), len - dengPos - 1);
pChRight[len - dengPos - 1] = '\0';
strValue.append(pChRight);


delete[] pChRight;


return true;
}
return false;
}


 

 

1 0
原创粉丝点击