Thread

NAOqi and NAOqi modules are thread safe. All modules created with module generator are not thread safe. User functions can be called in parallel but they have to be thread safe. You can use critical section to protect your functions.

Aldebaran provides tools to protect functions. Locking/unlocking mutex manually can be very dangerous because, sometimes we forget to unlock mutex or we cannot unlock it because an exception is raised.

Making function thread safe

Class Description
ALCriticalSection( pthread_rwlock_t * pMutex ) The block is thread safe as long as the ALCriticalSection object exists. If an exception is raised, the mutex is automatically unlocked.
ALCriticalSectionRead( pthread_rwlock_t * pMutex ), ALCriticalSectionWrite( pthread_rwlock_t * pMutex ) Same as ALCriticalSection but you can have as many readers that you want, as long as nobody writes.
ALCriticalFalseIfLocked(pthread_mutex_t *mutex, bool &alreadyLocked); Mutex will not be locked if it is already locked but parameter alreadyLocked will return true.
ALCriticalSection(ALPtr<ALMutex> mutex) ALMutex manage for you initialization and destruction of pthread mutex.
ALPtr<ALMutex> createALMutex() ALMutex constructor is private use static createALMutex instead.

Warning:

Pthread mutex must be reset and destroyed but all critical section constructors also accept ALMutex that are automatically reset and deleted.

//C++ sample void myCallBack() { ALCriticalSection section(mutex); // All following code is thread safe // ... return; // end of critical section here }

// C++ // other function that use same mutex void myCallBack2() { ALCriticalSectionFalseIfLocked section(mutex, val); if (val) { // mutex is alread locked, i don't want to execute the method return; } // All following code is thread safe // ... return; // end of critical section here }

Only DCM has a real-time thread in NAOqi.

scheduling policy: SCHED_FIFO scheduling priority: 90

All others thread from threadpool are created with following options to give priority to DCM:

pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) pthread_attr_setschedpolicy(&attr, SCHED_OTHER)

By default threadpool has 20 thread with 1310720 bytes of stack size. A post.call will execute function in existing thread from threadpool. Threadpool will automatically increase if use run more than 20 parallel tasks.





Copyright © 2010 Aldebaran-Robotics - All rights reserved