k_sleep() and C++


Kim B√łnderga rd <kim@...>
 

I posted this (slightly modified) on slack a week ago, but didn't really get any response (on my primary problem)
Hope this is better

I'm mixing CPP and threads.

I've made a C++ 'thread'-class wrapping some of the normal k_thread functions.

The 'entry' function is a static function within the class, using first argument to denote the actual instance of my thread object.Something like this:
From the constructor:
id = k_thread_create(&zThread,zStack, K_THREAD_STACK_SIZEOF(zStack),
runner,
this, p1, p2,
priority, 0, K_FOREVER);
The stack is defined in my thread class like this:
private:
K_KERNEL_STACK_MEMBER(zStack, 4000);
The static runner:
private:
static void runner(void *p0, void *p1, void *p2);void thread::runner(void* p0, void* p1, void* p2)
{
thread* inst = static_cast<thread*>(p0);
inst->runnerInst(p1, p2);
}
and the instance-specific runner:
private:
void runnerInst(void *p1, void *p2);voidt hread::runnerInst(void* p1, void* p2)
{
threadFunc(p1, p2); // This is a virtual function being overwritten by classes deriving from thread
}

threadFunc is a private method like this (example):
private:
void threadFunc(void* p1, void* p2) {
while (!stopCondition()) {
:
k_sleep(K_MSEC(1000));
}
}


It has been working fine on Zephyr 2.2 but now on Zephyr 2.4 I often see crashes with BUS ERRORs.
It seems to always come if a thread calls k_sleep() (but not everytime I call k_sleep())
Inspired by samples/cpp_synchronization I tried changing my k_sleep()'s to k_timer_status_sync() . It made my sample project work again.

I can live with that, but eventually I'll call something calling k_sleep()
Can anyone explain if I'm doing something wrong (and why) or is it a coincidence that k_timer_status_sync() approach is working?

/Kim B√łndergaard