We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi,
In keil.
void taskThread(void const * arg) {
}
osThreadDef(taskThread, osPriorityNormal, 1, 2500);
I want to use same function for multiple instance of osThreadId ( for 2 or more threads).
Can I attach function to thread at run-time.
So I can wrap in my C++ thread class and use thread class multiple instance. which can create multiple threads.
Thanks, Naeem
I am looking at this example
developer.mbed.org/.../Thread.cpp
My example is simple.
/* Forward declarations ----------------------------------------------------- */
/* Definitions -------------------------------------------------------------- */ class ThreadHandle;
/**************************************************************************//** \brief \details *******************************************************************************/ class ActiThread : public IActiThread { public: virtual ARLErrorCode_e start(); virtual ARLErrorCode_e stop(); virtual void sleep(u32 time); virtual void suspend(); virtual void resume(); virtual void setName(const char* name); virtual bool isEnabled(); public: IActiThreadObserver* getObserver(); private: ThreadHandle* thread_handle_; bool enabled_; ActiMutex mutex_; };
struct ThreadHandle { osThreadId thread_; };
ActiThread* acti_thread = (ActiThread*)arg; if(acti_thread == 0) { // Log error return; } IActiThreadObserver* observer = acti_thread->getObserver(); if(observer == 0) { // Log error return; } while(acti_thread->isEnabled()) { observer->runLoop(); } }
osThreadDef(taskThread, osPriorityNormal, 1, 2500); /***************************************************************************//** \brief \details \note \param[in] *******************************************************************************/ ARLErrorCode_e ActiThread::start() { thread_handle_ = new ThreadHandle; enabled_ = true; thread_handle_->thread_ = osThreadCreate(osThread(taskThread), this);
return ES_NoError; } /***************************************************************************//** \brief \details *******************************************************************************/ ARLErrorCode_e ActiThread::stop () { mutex_.lock(); enabled_ = false; mutex_.unlock(); osThreadTerminate(thread_handle_->thread_); delete thread_handle_; return ES_NoError; }
/***************************************************************************//** \brief Yield function returns remaining time slice to other threads \details *******************************************************************************/ void ActiThread::sleep (u32 time) {
} /***************************************************************************//** \brief Yield function returns remaining time slice to other threads \details *******************************************************************************/ void ActiThread::suspend() { osSignalWait (0x0001, osWaitForever); // wait forever for the signal 0x0001 } /***************************************************************************//** \brief Yield function returns remaining time slice to other threads \details *******************************************************************************/
void ActiThread::resume() { osSignalSet (thread_handle_->thread_, 0x0001); }
IActiThreadObserver* ActiThread::getObserver() { return observer_; } bool ActiThread::isEnabled() { bool is_enabled; mutex_.lock(); is_enabled = enabled_; mutex_.unlock(); return is_enabled; } void ActiThread::setName(const char* name) { }