Thursday, 15 January 2015

Global variable has different memory alignment in different threads -


Recently I had a memory alignment problem that crashed my application and in the end a global variable has different threads Got different alignment in The global variable is defined

  typedef struct {double e1; Double E2; Double E3; Double E4; Double E5; Double E6; Double E7; Double E8; Double E9; Unsigned long E10; Unsigned long E11; Float e 12; } Event_t;  

This is declared in main.c

  event_t report [3];  

and two threads (two files) were accessed by T1 and T2. T1 is the manufacturer and fills in the report and fills the data. T2 bus format reads out data for conversion, in T2, the content of the report [1] is always incorrect By checking the address of the report [1], I found that the report [1] has 4 bytes T1 and T2 I have changed the definition to:

  Typeface level {double e1; Double E2; Double E3; Double E4; Double E5; Double E6; Double E7; Double E8; Double E9; Unsigned long E10; Unsigned long E11; Float e 12; } __attribute__ ((pack)) event_t;  

Now both T1 and T2 are correct. Mute locks using T1 and T2 to reach global variables for synchronization. My question is why t1 is always true, but does T2 have different memory alignment for this global variable?

Thanks in advance.


No comments:

Post a Comment