69,372
社区成员
发帖
与我相关
我的任务
分享
int a=1234;
float b=12.34;
char a1[5];
char b1[6];
sprintf(a1,"%d",a);
sprintf(b1,"%f",b);
typedef struct __rantype
{
unsigned char status;
unsigned long ran_size;
unsigned long min_size;
unsigned long max_size;
}__rantype, __RANTYPE, *P__RANTYPE, **LP__RANTYPE;
DWORD WINAPI Trandom(PVOID f)
{
P__RANTYPE m_ran = (P__RANTYPE)f;
if( m_ran == NULL )
{
return -1;
}
if( (m_ran->status & 0x01) != 0x01 )
{
return 0;
}
::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_IDLE);
SYSTEMTIME m_time;
unsigned long r_char = 0;
while( (m_ran->status & 0x02) != 0x02 )
{
::GetLocalTime(&m_time);
r_char = 0;
m_time.wSecond += m_time.wMilliseconds;
if( (m_time.wMilliseconds & 0x0001) != 0x0001 )
{
++m_ran->ran_size;
Sleep(m_time.wMilliseconds);
}
if( m_ran->ran_size > m_ran->max_size )
{
::GetLocalTime(&m_time);
m_ran->ran_size = m_time.wSecond;
}
if( m_ran->ran_size < m_ran->min_size )
{
m_ran->ran_size = m_time.wSecond | (m_time.wMilliseconds << 16);
}
++m_ran->ran_size;
Sleep(1);
}
m_ran->status = 0;
return 1;
}
void* RandomInit(unsigned long m_Min = 0, unsigned long m_Max = 0xffffffff)
{
P__RANTYPE m_Random = (P__RANTYPE)malloc(sizeof(__RANTYPE));
if( m_Random == NULL )
{
return NULL;
}
m_Random->min_size = m_Min;
m_Random->max_size = m_Max;
m_Random->status = 0x01;
::CreateThread(NULL, NULL, Trandom, m_Random, NULL, NULL);
return m_Random;
}
void FreeRandom(void* m_Ranp)
{
if( m_Ranp == NULL )
{
return;
}
P__RANTYPE m_Random = (P__RANTYPE)m_Ranp;
m_Random->status = 0x01 | 0x02;
unsigned char m_Nber = 0;
while( m_Random->status != 0 && m_Nber < 254 )
{
++m_Nber;
Sleep(100);
}
free(m_Random);
}
unsigned long GetRandoms(void* m_Ranp)
{
if( m_Ranp == NULL )
{
return 0;
}
P__RANTYPE m_Random = (P__RANTYPE)m_Ranp;
return m_Random->ran_size;
}
typedef struct __lock{
unsigned char status;
unsigned char l_ber;
}__lock, __LOCK, *P__LOCK, **LP__LOCK;
#define BLOCK_NOT 0x00
#define BLOCK_OK 0x01
#define BLOCK_EXIT 0x02
P__LOCK LockInit( void )
{
P__LOCK l_hdr = (P__LOCK)malloc(sizeof(struct __lock));
if( l_hdr == NULL )
{
return NULL;
}
l_hdr->l_ber = 0;
l_hdr->status = BLOCK_NOT;
return l_hdr;
}
char Lock( P__LOCK m_lock )
{
if( m_lock == NULL )
{
return -1;
}
++m_lock->l_ber;
const unsigned char l_ber = m_lock->l_ber;
if( (m_lock->status & BLOCK_OK) == BLOCK_OK && m_lock->l_ber > 0 )
{
LockCf:NULL;
while( (m_lock->status & BLOCK_OK) == BLOCK_OK )
{
if( (m_lock->status & BLOCK_EXIT) == BLOCK_EXIT )
{
return 0;
}
Sleep(0);
}
if( m_lock->l_ber != l_ber )
{
goto LockCf;
}
}
m_lock->status |= BLOCK_OK;
return 1;
}
char Unlock( P__LOCK m_lock )
{
if( m_lock == NULL )
{
return -1;
}
--m_lock->l_ber;
m_lock->status &= (~BLOCK_OK);
return 1;
}