2017-03-06 16 views
0

을 비 블로킹 비동기식으로 변환하려면 어떻게해야합니까? 클라이언트와 서버 사이의 비동기 통신을 시도하고 있습니다. 여기 내 차단 싱크 코드입니다. 어떻게 비동기를 수행합니까?블로킹 코드를 비동기식으로 변환하는 코드

bool S3W::CImplServerData::WaitForCompletion(unsigned int timeout) 
{ 


    unsigned int t1; 
    while (true) 
    { 
     BinaryMessageBuffer currBuff; 
     if (m_Queue.try_pop(currBuff)) 
     { 
      ProcessBuffer(currBuff); 
      t1 = clock(); 
     } 
     else 
     { 
      unsigned int t2 = clock(); 

      if ((t2 - t1) > timeout) 
      { 
       return false; 
      } 
      else 
      { 
       Sleep(1); 
      } 
     } 
    } 

    return true; 
} 
+0

를 얻을? 특정 프레임 워크를 사용하고 있습니까? 일부 플랫폼 관련 기능? 정교하게하십시오! 그리고 [좋은 질문을하는 법을 읽으십시오] (http://stackoverflow.com/help/how-to-ask) 및 [최소한의 완전하고 검증 가능한 예제] (http : //stackoverflow.com/help/mcve). –

+0

OGR Api를 사용하고 있습니다. 내 게시물을 수정합니다 –

답변

0

이동 함수 자체의 외부 while 루프 :

bool S3W::CImplServerData::WaitForCompletion() 
{ 
    BinaryMessageBuffer currBuff; 
    if (m_Queue.try_pop(currBuff)) 
    { 
     ProcessBuffer(currBuff); 
     // do any processing needed here 
    } 

    // return values to tell the rest of the program what to do 
} 

메인 루프 while 루프는 "통신"을 어떻게해야합니까

while (true) 
{ 
    bool outcome = S3W::CImplServerData::WaitForCompletion() 

    // outcome tells the main program whether any communications 
    // were received. handle any returned values here 

    // handle stuff you do while waiting, e.g. check for input and update 
    // the graphics 
} 
+0

이 비동기 통신입니까? 그래서 많은 코드를 다시 작성해야하지 않습니까? –

+0

BTW 당신은 ​​멀티 스레드 용으로 갈 수 있지만, 이런 식으로 잔인합니다. try_pop은 이미 입력을 검사하는 비 차단 함수이므로 차단 문제를 해결할 수 있습니다. 이것은 단일 스레드 응용 프로그램에서 수행하는 방법입니다. –

+0

정말 고마워! –