2013-06-11 1 views
1

나는 C++ pthread에 대해 질문이있다.Obj-C performSelector OnThread in pthread C++

제가 Thread1과 Thread2를 가지고 있다면.

Thread1에서 호출 된 Thread2에서 Thread2 메소드를 실행하는 방법이 있습니까?

//code example 

//we can suppose that Thread2 call has a method 

void myThread2Method(); 

//I would to call this method from Thread1 but your execution must to run on Thread2.. 

thread1.myThread2Method() 

obj-c에있는 performSelector OnThread와 비슷한 방법이 있는지 알고 싶습니다.

+0

는 백그라운드 스레드에서 목표 - C에서 C++ 메소드를 호출하는 목표는 일할 수있는 방법에 기초를 보여주는 원유 예입니다? –

+0

C++ 메서드 호출을 * performSelector : onThread : *라고하는 Objective-C 메서드로 래핑 할 수 있습니다. –

+0

아니요, 어쩌면 ... 분명 obj-c가 사용되지 않았습니다. 난 단지 C + +를 사용하고 obj-c와 같은 것을 사용하고 싶습니다 – Safari

답변

1

순수한 pthread로는 이와 유사한 방법이 없습니다. 이것은 (당신이 말하는 objective-C 함수) run-loop를 가진 쓰레드에서만 작동하기 때문에 objective-C로 제한됩니다.

pure-c에는 실행 루프/메시지 펌프와 동일한 기능이 없으며, 이는 guis (예 : iOS 등)에 따라 다릅니다.

유일한 대안은 thread-2가 어떤 종류의 조건을 검사하고 설정되어 있다면 미리 정의 된 작업을 실행하는 것입니다. (이것은 전역 함수 포인터 일 수 있습니다. 포인터가 null이 아니면 thread-2가 함수를 주기적으로 검사하고 실행합니다). 여기

는이

void (*theTaskFunc)(void); // global pointer to a function 

void pthread2() 
{ 
    while (some condition) { 
     // performs some work 

     // periodically checks if there is something to do 
     if (theTaskFunc!=NULL) { 
      theTaskFunc();  // call the function in the pointer 
      theTaskFunc= NULL; // reset the pointer until thread 1 sets it again 
     } 
    } 
    ... 
} 

void pthread1() 
{ 

     // at some point tell thread2 to exec the task. 
     theTaskFunc= myThread2Method; // assign function pointer 
} 
+0

제가 제 질문에 가장 가까운 대답이라고 생각합니다. 그래도 내가하고 싶지 않은 것이지요. – Safari