Helpful ReplyHot!thread safety in C language

Author
newb77777
Junior Member
  • Total Posts : 97
  • Reward points : 0
  • Joined: 2015/04/04 01:46:02
  • Location: 0
  • Status: offline
2018/02/07 23:52:54 (permalink)
0

thread safety in C language

can anyone explain thread safety with an example?
wiki: 
  • Thread safe: Implementation is guaranteed to be free of race conditions when accessed by multiple threads simultaneously.
race condition is a concept in digital Electronics and happens due to long paths and short paths. 
how can sequential logic have race???
#1
PStechPaul
Super Member
  • Total Posts : 1835
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: thread safety in C language 2018/02/08 03:03:07 (permalink) ☄ Helpfulby newb77777 2018/02/08 13:21:14
5 (2)
I don't know if threads apply to PICs, but perhaps the PIC32 might implement a real time operating system (RTOS) like Windows. Threads are used to allow multiple processes to run concurrently, but with a single processor that only appears to be the case. In reality each process has a prioritized slice of CPU time, and operation is transferred back and forth. Threads are similar to interrupts and ISRs, which take care of background tasks such as communications and ADC conversions while allowing the main process to run with minimal interruption. But there are some processes that cannot be safely interrupted, such as precise delays, so you may need to disable interrupts during that time. For threads, there are mechanisms such as "critical sections" and "mutexes" (for mutually exclusive processes).

 
#2
JPortici
Super Member
  • Total Posts : 216
  • Reward points : 0
  • Joined: 2012/11/17 06:27:45
  • Location: 0
  • Status: online
Re: thread safety in C language 2018/02/08 03:04:19 (permalink)
5 (2)
you have a resource, thread 1 is reading data from resource.
thread is interrupted and thread 2 wants to write to resource.
thread 1 resume excecution, now data has changed.
 
should it have changed, or should it have remained the same until thread 1 had "released" it?
 
It doesn't happen in RTOS only..
imagine if you are operating on a variable, normal program flow is halted because of an interrupt and inside the interrupt the value of the variable is changed. what happens next?
 
post edited by JPortici - 2018/02/08 03:07:16
#3
qɥb
Monolothic Member
  • Total Posts : 2635
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: thread safety in C language 2018/02/08 04:04:54 (permalink)
0
newb77777
can anyone explain thread safety with an example?

As Paul mentioned, this only applies if you are running some sort of RTOS (Real Time Operating System).
If you're writing your own C code, with no RTOS, then it's entirely irrelevant.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#4
NKurzman
A Guy on the Net
  • Total Posts : 15945
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: thread safety in C language 2018/02/08 06:35:48 (permalink) ☄ Helpfulby newb77777 2018/02/08 13:20:51
5 (2)
No I disagree, it is also relevant if interrupts are used.
#5
1and0
Access is Denied
  • Total Posts : 8001
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: thread safety in C language 2018/02/08 07:48:22 (permalink)
5 (1)
NKurzman
No I disagree, it is also relevant if interrupts are used.

Agree. Interrupt can be considered as a thread.
#6
MicroDawg
Go for a walk!?
  • Total Posts : 100
  • Reward points : 0
  • Joined: 2015/12/04 15:06:27
  • Location: Chandler, AZ
  • Status: offline
Re: thread safety in C language 2018/02/08 08:33:14 (permalink) ☄ Helpfulby newb77777 2018/02/08 13:19:06
5 (2)
To stop the interrupt from messing with your changes:
pseudo code:
saved_interrupt_enable_state = interrupt_enable_state_get();
interrupt_enable_state_set(disable);
~~ MODIFY SHARED DATA ~~
interrupt_enable_state_set(saved_interrupt_enable_state);

 
#7
NorthGuy
Super Member
  • Total Posts : 4876
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: offline
Re: thread safety in C language 2018/02/08 08:59:12 (permalink) ☄ Helpfulby newb77777 2018/02/08 13:20:41
5 (3)
Some sort of schedule is doing time-slicing, so small pieces of code from different threads are executed sequentially. This creates an illusion of parallel execution. Since the order of execution is generally unpredictable, this creates a race condition - you don't know which piece of code (possibly accessing common variables) executes first.
 
C functions are generally thread-safe unless they access some common data (i.e. global or static variables). However, when common data needs to be accessed, thread-safety comes at a cost - other threads must be stopped or blocked for a while (such as MicroDawg suggestion) and if the threads being stopped are time-critical then you get problems.
 
#8
naeem1234
Super Member
  • Total Posts : 358
  • Reward points : 0
  • Joined: 2015/02/19 06:39:28
  • Location: 0
  • Status: offline
Re: thread safety in C language 2018/02/21 01:01:18 (permalink)
0
MicroDawg
To stop the interrupt from messing with your changes:
pseudo code:
saved_interrupt_enable_state = interrupt_enable_state_get();
interrupt_enable_state_set(disable);
~~ MODIFY SHARED DATA ~~
interrupt_enable_state_set(saved_interrupt_enable_state);




where can these functions be found in XC8 ?
 
interrupt_enable_state_get() and interrupt_enable_state_set() 
 
Also what is data type for 'saved_interrupt_enable_state' ?
 
#9
1and0
Access is Denied
  • Total Posts : 8001
  • Reward points : 0
  • Joined: 2007/05/06 12:03:20
  • Location: Harry's Gray Matter
  • Status: offline
Re: thread safety in C language 2018/02/21 03:12:37 (permalink)
5 (1)
naeem1234
where can these functions be found in XC8 ?
 
interrupt_enable_state_get() and interrupt_enable_state_set() 
 
Also what is data type for 'saved_interrupt_enable_state' ?

Those are pseudo code, as he has stated in his post.  Here's what it can be in "real" C code:
uint8_t saved_interrupt_enable_state = GIE;
GIE = 0;
~~ MODIFY SHARED DATA ~~
GIE = saved_interrupt_enable_state;

 
#10
NKurzman
A Guy on the Net
  • Total Posts : 15945
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: thread safety in C language 2018/02/21 06:41:04 (permalink)
4 (1)
XC8 has a bit type.
#11
crosland
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re: thread safety in C language 2018/02/21 09:12:36 (permalink)
5 (1)
newb77777
can anyone explain thread safety with an example?
wiki: 
  • Thread safe: Implementation is guaranteed to be free of race conditions when accessed by multiple threads simultaneously.
 
Rather than picking out a single quote like that, I suggest you read the whole article which explains thread safety quite nicely. Come back if there is still something you do not understand.
 
#12
Jump to:
© 2018 APG vNext Trial Version 4.5