Skip navigation.
Home
The QNX Community Portal

View topic - avoiding pre-emption

avoiding pre-emption

For discussion of realtime and/or embedded programming.

avoiding pre-emption

Postby rahil » Mon Dec 21, 2009 9:06 am

Hi!

I wanted to know some API which allows me to set the value of variable, for example,

set_me = TRUE

without being pre-empted.
Actually, I have two threads viz a main thread and a timer thread, which gets invoke every second. The timer thread, every tick (say 1 ms) checks 2-3 test_me variables and based upon them change the value of 2 set_me variables. This timer thread may change the value of the set_me only in 15 - 30 ms (15-30 timeth the Timer Thread gets scheduled, it changes the set_me variable). Since the timer thread gets scheduled so often, I am reluctant to use mutex here.

So, any suggestions on how should I avoid any race conditions if possible.
I thought atomic_set could be the solution but I've no way to test it.
rahil
Active Member
 
Posts: 75
Joined: Wed Feb 11, 2009 12:13 pm
Location: India

Postby Tim » Mon Dec 21, 2009 4:17 pm

Rahil,

Atomic_set() is fine for what you want to do.

Tim
Tim
Senior Member
 
Posts: 1389
Joined: Wed Mar 10, 2004 12:28 am

Postby mario » Mon Dec 21, 2009 4:46 pm

And using a few mutexes every ms it no worry at all, they are very lightweight. When there is not conflict it is only a few assembly instructions. It is only when waiting is required that the kernel is involved.

If you are a a single core machine it's extremely unlikely to happened.

attomic_set may not do what you want because it only sets bits and does not reset them.

An operation like a = b; is atomic and doesn't need mutex. Assuming the variable is of not of type long long which isn't native. It's always a good idea to set variable 'a' to volatile though. However ++a or a = a + 1 is not atomic because it requires reading and writing.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby rahil » Tue Dec 22, 2009 4:48 am

mario wrote:And using a few mutexes every ms it no worry at all, they are very lightweight. When there is not conflict it is only a few assembly instructions. It is only when waiting is required that the kernel is involved.
.


I'll test with the mutex. One thing I want to ask though, will it be a good strategy to lock the mutex by the main thread just before setting the set_me variable, and unlocking the mutex soon after that? for e.g

pthread_mutex_lock (&myMutex)
set_me = TRUE
pthread_mutex_unlock (&myMutex)


Or should I lock the mutex just at the start of main thread function and unlock it at the end and similarly for the timer thread. So, if any one of the thread has acquired the mutex, another thread won't be able to run, which is undesirable. Please validate my understanding.
Last edited by rahil on Tue Dec 22, 2009 4:51 am, edited 1 time in total.
rahil
Active Member
 
Posts: 75
Joined: Wed Feb 11, 2009 12:13 pm
Location: India

Postby rahil » Tue Dec 22, 2009 4:50 am

Tim wrote:Rahil,

Atomic_set() is fine for what you want to do.

Tim

Will try setting set_me to TRUE using atomic_set() also, lets see if it works out.
rahil
Active Member
 
Posts: 75
Joined: Wed Feb 11, 2009 12:13 pm
Location: India

Postby mario » Tue Dec 22, 2009 1:21 pm

rahil wrote:
mario wrote:And using a few mutexes every ms it no worry at all, they are very lightweight. When there is not conflict it is only a few assembly instructions. It is only when waiting is required that the kernel is involved.
.


I'll test with the mutex. One thing I want to ask though, will it be a good strategy to lock the mutex by the main thread just before setting the set_me variable, and unlocking the mutex soon after that? for e.g

pthread_mutex_lock (&myMutex)
set_me = TRUE
pthread_mutex_unlock (&myMutex)


This is fine as long as you also do the same when reading it, but as I said for an operation such as set_me=TRUE there is no need for mutexes this is an atomic operation.
mario
QNX Master
 
Posts: 4132
Joined: Sun Sep 01, 2002 1:04 am

Postby Tim » Tue Dec 22, 2009 8:14 pm

Rahil,

Of course if you have a

set_me1 = TRUE;
set_me2 = TRUE;

and both variables need to be protected then the atomic stuff won't work and while each statement is an atomic operation both combined aren't.

I would personally use mutexes simply because it makes it abundantly clear these are shared variables to anyone looking at the code.

Tim
Tim
Senior Member
 
Posts: 1389
Joined: Wed Mar 10, 2004 12:28 am


Return to Realtime and Embedded

Who is online

Users browsing this forum: No registered users and 2 guests

cron