2011-02-09 6 views

答えて

1

いいえ、自分でロールバックする必要があります。これを行う1つの方法は、メインスレッドがメインループの各繰り返しをチェックするグローバル関数ポインタを保持することです。例:Windowsの代わりにPOSIXのために

// Global variables 
pthread_mutex_t gMutex = PTHREAD_MUTEX_INITIALIZER; 
void (*gFuncPtr)(void*); 
void *gFuncArg; 

// Your program's main loop in the main thread 
while(programIsRunning()) 
{ 
    // If we have a function to call this frame, call it, then do the rest of 
    // our main loop processing 
    void (*funcPtr)(void*); 
    void *funcArg; 

    pthread_mutex_lock(&gMutex); 
    funcPtr = gFuncPtr; 
    funcArg = gFuncArg; 
    gFuncPtr = NULL; 
    pthread_mutex_unlock(&gMutex); 

    if(funcPtr != NULL) 
     (*funcPtr)(funcArg); 

    // Rest of main loop 
    ... 
} 

// Call this function from another thread to have the given function called on 
// the main thread. Note that this only lets you call ONE function per main 
// loop iteration; it is left as an exercise to the reader to let you call as 
// many functions as you want. Hint: use a linked list instead of a single 
// (function, argument) pair. 
void CallFunctionOnMainThread(void (*funcPtr)(void*), void *funcArg) 
{ 
    pthread_mutex_lock(&gMutex); 
    gFuncPtr = funcPtr; 
    gFuncArg = funcArg; 
    pthread_mutex_unlock(&gMutex); 
} 

は、それぞれその代わりpthread_mutex_tpthread_mutex_lock、およびpthread_mutex_unlockCRITICAL_SECTIONEnterCriticalSection、およびLeaveCriticalSectionを使用し、適切にクリティカルセクションを初期化し、deinitializeすることを忘れないでください。

+0

ためにブーストを使用しています。私はとても錆びています... – LucasTizma

0

iPhoneの場合は、CFRunLoopAddSource()を使用するか、Objective C++ファイル(拡張子.mm)を使用してperformSelectorOnMainThreadにC++に優しいラッパーを提供できます。私は第二の方法をお勧めします。

プラットフォームが異なる場合は、プラットフォームを指定してください。 Windowsにはそのような手段(APCとメッセージ)があります。

プラットフォームに依存しないスレッドは存在しないため、プラットフォームに依存しない方法はありません。

0

事はUNIXとWindowsの両方で実行する必要があります(あなたがC++を使用している)場合は、私はC++を欠場スレッド&ミューテックス

関連する問題