Question about shared variables
Laurent R
Posts: 27
Hi all
Here is my goal :
- main cog receive command like launch a new cog to do a task in backgound
- this second cog need to know some variables from the main code so I give him as parameters when he starts
First question : is it the right way or not ?
Second : If the second cog changes a variable, will the first cog see this modification?
Thanks for help
Laurent
Here is my goal :
- main cog receive command like launch a new cog to do a task in backgound
- this second cog need to know some variables from the main code so I give him as parameters when he starts
First question : is it the right way or not ?
Second : If the second cog changes a variable, will the first cog see this modification?
VAR Byte AnalogDefaultChan Byte DefaultRange Byte DefaultBip long ValAlarmHi Byte High_Pos long ValAlarmLo Byte Low_Pos Byte Latch_Alarm byte Warning_1270_flag
CogAlarm := cognew(alarm.Start(Warning_1270_flag,ValAlarmHi,High_Pos,ValAlarmHLo,Low_Pos,Latch_Alarm),@15)
Thanks for help
Laurent
Comments
Second, no, the second cog is operating on its own private copies of the first cog's variables. If you want to transmit information from the second cog to the first, you could pass the address of the variables, like so:
cognew(alarm.Start(@Warning_1270_flag,@ValAlarmHi, etc etc)
but you'd have to modify the Start method accordingly.
http://forums.parallax.com/showthread.php?p=765086
The compiler sorts global variables in an object according to their base size. Longs are first, then words, then bytes. If you're going to pass groups of variables of the same base size, you can just pass the address of the first variable and reference the others as if you were passing an array (using BYTE / WORD / LONG like LONG[noparse][[/noparse] variable address ] [noparse][[/noparse] subscript ]). You can also pass the addresses of each of the variables if there are only a few.
I'll see that
edit :
@ Mike : And if cognew is in the other object?
so I call "alarm.start" and the start method open a new cog it's the right way , is'nt it?
Best regards
Laurent
Post Edited (Laurent R) : 4/10/2009 9:01:13 AM GMT