2012-09-06 5 views
0

쉘 출력 아래의 코드를 실행하면 스레드 0과 스레드 1간에 겹칠 수 있습니다. 그래서 각 스레드가 출력 쓰기를 완료하는지 확인하는 가장 좋은 방법은 무엇입니까? 다른 하나가 출력을 시작하기 전에?IO를 OpenMP 섹션과 동기화하는 방법

이렇게하면 출력물이 깨끗한 지 확인할 수 있습니다.

미리 감사드립니다. 당신은 전용 섹션을 확인하는 것이 중요 섹션 이름을 사용할 수 있습니다

#pragma omp parallel num_threads(2) shared(emperor) 
{ 
    #pragma omp sections 
    { 
     #pragma omp section 
     { 
      #pragma omp critical (cout) 
       cout << "Open the listening thread by using openmp!" << endl; 
      emperor->startServerListening(); /// does some work 
     } 
     #pragma omp section 
     { 
      #pragma omp critical (cout) 
       cout << "Open the master thread by using openmp!" << endl; 
      emperor->startServerCoupling(); /// does some other work 
     } 
    } /// End of sections 
} /// End of parallel section 

, 즉 이름 : 한 번에 하나의 스레드가 출력에 기록하도록

#pragma omp parallel num_threads(2) shared(emperor) 
    { 
#pragma omp sections 
     { 
#pragma omp section 
      { 
       cout << "Open the listening thread by using openmp!" << endl; 
       emperor->startServerListening(); /// does some work 
      } 
#pragma omp section 
      { 
       cout << "Open the master thread by using openmp!" << endl; 
       emperor->startServerCoupling(); /// does some other work 
      } 
     } /// End of sections 
    } /// End of parallel section 

답변

0

가장 간단한 방법은, 크리티컬 섹션을 사용하는 것입니다 섹션은 동일한 이름을 가진 모든 섹션 (한 번에 한 섹션에 하나의 스레드)에 배타적입니다. 이름없는 섹션은 다른 모든 이름없는 섹션에만 적용됩니다.

관련 문제