Global Variables

Moderators: TomKerekes, dynomotion

Post Reply
AlMoutaz_Billah
Posts: 2
Joined: Wed Apr 29, 2020 11:56 am

Global Variables

Post by AlMoutaz_Billah » Wed Apr 29, 2020 12:18 pm

Maybe it is an old question, but I am facing a situation where I have a big structure (64 words) and I want it to be shared with all C files/threads, so is there any way other than using persist variables array method?
anyway I don't want the data to persist, I am using this structure everywhere (reading & writing) so I don't want to keep getting it from the persist data array and loading it back just to modify a bit or two.
I tried to use the header file declaration and the extern keyword with no success.
I hope I am clear enough here.
Thanks in advance.

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: Global Variables

Post by TomKerekes » Wed Apr 29, 2020 3:22 pm

Hi AlMoutaz,

You can use the 8MByte gather_buffer to store data.

Or you can create data structures within one Thread and pass the address of the data structure to the other Threads through a persist variable. See the examples InterCommunicateThread1.c and InterCommunicateThread2.c

Code: Select all

#include "KMotionDef.h"

int MyData[300]; // declare some data to share
int *pMyData=MyData; // declare a pointer to the data

main()
{
	int result=0;
	//give other Threads the address by passing in persist variable 10
	persist.UserData[10] = pMyData; 

	while(1){ 
		Delay_sec(1.0);
		pMyData[299] = result++;
		if(result>10000) result=0;
  	}
}

Code: Select all

#include "KMotionDef.h"

int *pMyData; // declare a pointer to the data

main()
{
	int msg;
	pMyData=persist.UserData[10]; //get address from other Thread
	
	while(1){ 
		Delay_sec(0.5);
		msg = pMyData[299];
		printf("msg:%d\n", msg);
	}
}
Regards,

Tom Kerekes
Dynomotion, Inc.

AlMoutaz_Billah
Posts: 2
Joined: Wed Apr 29, 2020 11:56 am

Re: Global Variables

Post by AlMoutaz_Billah » Wed Apr 29, 2020 7:26 pm

Thank you, Tom, for your fast reply, I will test your solution tomorrow, I hop. :)

Post Reply