System::Threading::Sleep

7 views
Skip to first unread message

sriram reddy

unread,
Feb 20, 2009, 10:54:22 AM2/20/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi,

I am trying to impliment a System.Threading.Sleep(8) function and i am
expecting to return thead after 8 millsecs.

However thead is inly rurning after 15 mills on XP pcs and working as
it should on vista.

Reason i figure out so far is the System is only updating time only
ones in every 15 mills. I verified by calling SYSTIME structure from
stdlib call;

To solve the problem i use StopWatch class to loop untill elapsed time
to reach 8 mills which delays running thead to waite accurate 8 mills.
But this is causing processor usage to 99% which is blocking other
threads to do their job.


Here is the code block causing the problem
===================================

void ACTSound::SendAudioOut(){


array<unsigned char>^ ml_data_list;
array<unsigned char>^ ml_data_chunk = gcnew array<unsigned char>(64);

DateTime^ ml_T_last_sent;
DateTime^ ml_now;


while(true){

if(m_data_out->Length != 0){
m_mutex->WaitOne();
ml_data_list = m_data_out;
m_mutex->ReleaseMutex();
for(int i =0;i<ml_data_list->Length; i=i+64)
{
if((ml_data_list->Length-i)>= 64){
m_mutex->WaitOne();
System::Array::Copy(ml_data_list,i,ml_data_chunk,0,64);
m_mutex->ReleaseMutex();
}
else{

m_mutex->WaitOne();
System::Array::Copy(ml_data_list,i,ml_data_chunk,0,(ml_data_list-
>Length-i));
m_mutex->ReleaseMutex();

}

if((System::Environment::OSVersion)->Version->Major >= 6){
m_media_skt->UpDateAudioOut(ml_data_chunk,ml_data_chunk-
>Length);
m_th_audio_out->Sleep(8);// if vista no problem

}
else{

Delay8ms(); // a waiting mechanisam for XP pc with 15 mills
granularituy
}

}
}
}

}
void ACTSound::Delay8ms()
{
System::Diagnostics::Stopwatch^ stopwatch =
System::Diagnostics::Stopwatch::StartNew();
stopwatch->Start();
//waite 8 milli secs
while (stopwatch->IsRunning)
{
if(stopwatch->ElapsedMilliseconds >=8){
stopwatch->Stop();
stopwatch->Reset();
}
else{

System::Threading::Thread::CurrentThread->Sleep(1); // this bit is
causing to wake up thread after 15 mills is i take this off system is
going 100 % usage.
}
}
}

Brandon Betances

unread,
Feb 20, 2009, 12:33:37 PM2/20/09
to DotNetDe...@googlegroups.com
wrong group.

Joe Enos

unread,
Feb 20, 2009, 12:52:31 PM2/20/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
I could be wrong, but isn't this still a .NET question? The question
seems to be about the System.Threading.Thread, and even though it's
written in MC++, I would expect the same scenario if the sample was
written in C# or VB.NET.

(FYI: I don't know the answer to the original question...)

On Feb 20, 10:33 am, Brandon Betances <bbetan...@gmail.com> wrote:
> wrong group.

sriram reddy

unread,
Feb 20, 2009, 1:40:12 PM2/20/09
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Sorry about that posting MC++ code insted fo c# code. But i was only
pointing the functionality of System.threading.thread.sleep(mill sec)
or sleep(TImeSpan(int64 ticks)) .

I can give sudo code in c#
-------------------------------------------

void SendData(array bigchunkdata)
{

int smallchunksize = 64;
byte[] smallchunkdata = new array(smallchunksize) ;

for( int i =0; i<= bigchunkdata.length; i=i+smallchunksize)
{

System.Array.Copy(bigchunkdata,i,smallchunkdata ,
0,smallchunksize);
MyScoketObj.Send(smallchunkdata );

if(Osversion >=6)
System.threading.thread.currentthread.sleep(8); // this
sleeping works on vista OS i.e Osverssion.major >=6
else
waite8mills();
}

void waite8mills()
{
StopWath mystopwatch = StopWatch.createnew();

while(mystopwatch->elapsedtime <8)
{

// DO nothing // this bit causing CPU usage
any function to make not to use CPU appriciated Sleep(1 to 15)
returning after minuimum 15 mils because of minimum tick time in OS is
15 mills
}
}

//other way of implimenting waite8mills

void waite8mills()
{
LARGE_INTEGER ticksPerSecond;
LARGE_INTEGER tick;

long one_ms =0;
long wait_until =0;

QueryPerformanceFrequency(ref ticksPerSecond );

one_ms = ticksPerSecond.QuadPart/1000;

QueryPerformanceCounter( ref tick);

wait_until = (8*one_ms) + tick.QuadPart;

while (tick.QuadPart < one_until )
{
CurrentThread.Sleep(System::TimeSpan(8*one_ms));
QueryPerformanceCounter(ref tick);
}
}

In both ways fo waiting causing eaither 15 mills of delay on sleep or
100% CPU usage .


Any suggestinons for wating accurate time or sleeping accurate time
wiath out causing CPU usage.


Thanks



Reply all
Reply to author
Forward
0 new messages