Multithreading details with DLL

Questions about MultiCharts and user contributed studies.
Terabyte
Posts: 8
Joined: 06 Jun 2012

Multithreading details with DLL

Postby Terabyte » 16 Jun 2012

I am trying to understand more about exactly how MC64 handles multithreading.

FIRST SCENARIO:
a) I have 2 identical charts.
b) Both charts have same identical study which calls DLL functions "SetDLLVariable", "IncrementDLLVaraible", "GetDLLVariable". One function sets a variable inside the DLL to the specified value, the other function increments this same variable by 1 each time it is called, and finally the last one Gets the variable from the DLL memory.
c) Assume the DLL shares this single variable as shared global memory between all charts, all applications, and system wide.
d) Assume the PowerLanguage code is:

SetDLLVariable(0);
IncrementDLLVariable; //1
IncrementDLLVariable; //2
IncrementDLLVariable; //3
IncrementDLLVariable; //4
IncrementDLLVariable; //5
Print( GetDLLVariable() );

d) If this was a single chart, the printed value will always be exactly 5 at the end of each tick update.
e) However, with two charts, each chart is run it its own thread.

QUESTION 1:
1. Could both charts be updating the variable at the same time?
2. Could the printed value sometimes be more than 5 since both charts may be updating the variable at the same time?

SECOND SCENARIO:
a) Exactly the same as above, EXCEPT the DLL is NOT globally shared variable. Rather each instance of the DLL is independant of one another. So the variable is NOT shared between applications or system wide.

QUESTION 2:
Does each chart create its own instance of the DLL automatically like two different processes?
Could the printed value sometimes be more than 5 since both charts may be updating the variable at the same time?

User avatar
Dave Masalov
Posts: 1712
Joined: 16 Apr 2010
Has thanked: 51 times
Been thanked: 485 times

Re: Multithreading details with DLL

Postby Dave Masalov » 18 Jun 2012

Hello Terabyte,

QUESTION 1:
1. Could both charts be updating the variable at the same time?

Yes.

2. Could the printed value sometimes be more than 5 since
both charts may be updating the variable at the same time?

Yes

QUESTION 2:
Does each chart create its own instance of the DLL automatically like two different processes?

Loading dll in a process is managed by the Operation System and is not related to a particular chart.

Could the printed value sometimes be more than 5 since both charts may be updating the variable at the same time?

If the charts belong to the one MultiCharts.exe process (opened in one instance of MultiCharts), then yes. If the charts below to different MultiCharts.exe processes, then no.

janus
Posts: 755
Joined: 25 May 2009
Has thanked: 41 times
Been thanked: 84 times

Re: Multithreading details with DLL

Postby janus » 19 Jun 2012

In such cases where global variables are accessed by various studies (which I presume are multi- threaded) and there's a thread collision what happens? I'm hoping MC uses some kind of thread locking mechanism. I use critical sections in my own multi-threaded DLLs to avoid such collisions when they are called by multiple studies at the same time. I've noticed on occasion if I don't use any locking approach MC may crash. I think this happens if one study calls my DLL that tries to write to a shared variable (defined in my own DLL as a shared variable so the scope of that variable is not global in the true sense as it's not directly visible to MC; only shared in the DLL that contains various functions entry points) while another tries to read or write to it at the same time. I'd be interested to know what MC uses for global or locally shared variables to handle thread collisions correctly.

Terabyte
Posts: 8
Joined: 06 Jun 2012

Re: Multithreading details with DLL

Postby Terabyte » 19 Jun 2012

Dave,

Thank you for your reply. Here are a few more question to help me clarify the handling of multithreading and MC64:

1) Using the previously mentioned "First Scenerio" example, what is the approach so there is no chance another chart or another insertion of same indicator on same chart will interfere with one another and the number will always be guaranteed to be 5?

I'm not looking for the actual coding, rather the high level steps "How to" handle the threads, avoiding data collisions, avoiding interference from another chart or another occurrence of same indicator using same dll.

Thank you.

janus
Posts: 755
Joined: 25 May 2009
Has thanked: 41 times
Been thanked: 84 times

Re: Multithreading details with DLL

Postby janus » 19 Jun 2012

One way is to identify uniquely each study with a code, and test it to filter which study use the global variable calls and which don't. I use
text(getsymbolname,"_",getstrategyname,"_",getappinfo(aiappid):0:0,"_",basedatanumber:0:0)
I then execute certain actions only for a study or studies that match the code.

Another approach is to define an input variable and set it to 1 for only those studies you want the actions executed, and all the others to 0. Then test for this variable in your code to execute the actions you want.
These users thanked the author janus for the post:
TJ


Return to “MultiCharts”