Broker API - How to freeze zorro with multithreading & callbacks

Posted By: AndrewAMD

Broker API - How to freeze zorro with multithreading & callbacks - 11/27/17 00:41

I'm not sure if this is a "lessons learned" thread or a "feature request" thread. I'll let jcl decide if he wants. laugh

Here's a code snippet that will freeze Zorro from the broker DLL plugin:

Code:
// various headers omitted
#define PLUGIN_VERSION	2
////////////////////////////////////////////////////////////////
extern "C" __declspec(dllexport) BrokerOpen(char* Name, FARPROC fpError, FARPROC fpProgress)
{
	strcpy_s(Name, 32, "TEST Callback");
	(FARPROC&)BrokerError = fpError;
	(FARPROC&)BrokerProgress = fpProgress;
	return PLUGIN_VERSION;
}

extern "C" __declspec(dllexport) BrokerLogin(char* User, char* Pwd, char* Type, char* Accounts)
{
	std::thread tcp_listener_thread([&]() {
		BrokerProgress(0); // I want to trigger a tick read on a dedicated listener thread.
		BrokerError("hello world");
	});
	tcp_listener_thread.join();
	return 0;
}



So what happened here?

1) BrokerError and BrokerProgress both invoke Zorro functions.
2) Zorro invokes BrokerLogin on this test DLL.
3) BrokerLogin starts a new thread tcp_listener_thread.
4) BrokerLogin waits for the thread to return & rejoin.

Meanwhile at tcp_listener_thread...
1) BrokerProgress tries to call Zorro. It freezes here because the zorro thread is occupied with BrokerLogin.
2) If BrokerProgress were commented out, the thread would freeze at the very next line for the same exact reason as the last.

In practice, a listener thread would make callbacks when the Zorro thread is occupied or not occupied. A plugin writer would have to account for both cases.

Possible workarounds involve forcing callbacks on the Zorro thread only, or some combination of std::atomic<bool> and using class instances whose destructors indicate thread availability, for example.

In conclusion, I think it would be nice if one of these two things happened:
1) Revise the manual to indicate that callbacks should only be issued when the zorro thread is unoccupied and recommend workarounds, or
2) A dedicated Zorro thread listens to / queues broker DLL callbacks.
Posted By: jcl

Re: Broker API - How to freeze zorro with multithreading & callbacks - 11/27/17 07:28

Yes. Zorro functions can only be called in the Zorro thread. Not in a different thread. For some functions, it may work, but not for functions affecting the message loop, such as BrokerProgress.
Posted By: AndrewAMD

Re: Broker API - How to freeze zorro with multithreading & callbacks - 05/05/18 14:54

jcl, following up from this old thread...

This is from the description of fpProgress:
Quote:
When the progress parameter is 0, Zorro will trigger a cycle for requesting a price quote.
Am I allowed to call BrokerProgress(0) from a separate thread? And at any time?

And to be clear, this is how I tell Zorro that I have new market data ready to be read, correct?

EDIT: Okay, so I found this:
http://www.opserver.de/ubb7/ubbthreads.p...true#Post459055

But there is no discussion regarding threads, so I am still unsure.

EDIT2: And if not, how am I supposed to be able to push data while on the Zorro thread?
Posted By: jcl

Re: Broker API - How to freeze zorro with multithreading & callbacks - 05/08/18 10:31

I believe brokerProgress(0) must not be called from a separate thread. At least there is no mechanism that would allow calling it from another thread.
Posted By: AndrewAMD

Re: Broker API - How to freeze zorro with multithreading & callbacks - 05/08/18 11:16

Do any of the other plugins trigger a price cycle? If so, how is it used, and under which DLL call? And finally, what is achieved by triggering the cycle from a Zorro thread?
© 2024 lite-C Forums