Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-02

Can a library synchronize foreign procedure ?

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Fred van Stappen

Hello everybody.

The question is certainly stupid because i guess it is impossible, but who knows...

Is is possible for a library to synchronize a procedure form the program who use that library ? I guess no but does it exist a "universal" way for a library to sent message to the program who use it ?

Many thanks.

Michael Schnell
to Fred van Stappen
Fred van Stappen wrote
Is is possible for a library to synchronize a procedure form the program who use that library ?

What exactly do you mean by "synchronize".

In the FPC-RTL we have TThread.Synchronize.

This means:
- Push an event on the Event Queue to have it executed by the Main Thread
- Have this thread wait until the main thread is done.

Now after calling "Synchronize" the current (worker) thread stops. It can last a long time until the main thread is ready to fetch and execute the Event. After it did that the worker is freed and goes on running, while the main thread fetches the next Event from the queue.

In theory it should be possible to start a thread in a library and do the stuff synchronize does.

But as TThread is an Object and Objects are implemented on the heap, I suppose that you need synchronized heaps between the library and the main program.

AFAIK, there is a way to have a library (if written with fpc) "attach" to the main program's heap, but I don't know how this is done.

Another way would be to define the complete Thread object in the main program and from there just call normal (flat) functions in the library, but I suppose this is not what you intend.

-Michael

Fred van Stappen
to Michael Schnell
But as TThread is an Object and Objects are implemented on the heap,

I

suppose that you need synchronized heaps between the library and

the

main program.

Exactly, that is what i want...

AFAIK, there is a way to have a library (if written with fpc)

"attach" to the

main program's heap, but I don't know how this is

done.

Another way would be to define the complete Thread object in the

main

program and from there just call normal (flat) functions in the

library, but I

suppose this is not what you intend.

Hum, not really, i prefer to let the threat-job done by the library, not ask to the developer to reinvent the hot water... ;-)

So, what i will do (till i find a other solution), is to use a timer inside the main program who will check, calling the library, if there is something to be executed...

Many thanks Michael.

Fred

Michael Schnell
to Fred van Stappen
Fred van Stappen wrote 6 lines
> you need synchronized heaps between the library and the >main program. Exactly, that is what i want...

Unifying the heap is known to be necessary to successfully pass string parameters between the main program and the library. Supposedly also Objects "free" and open arrays will not work as expected.

I once did that with Delphi (pre XE). It was easy.

But I have no idea how this is done with the fpc heap manager (though I seem to remember having read about it some time ago).

Of course as it needs to work in multiple OSes with different dynamic library paradigms, it's more complicated than in pure Windows Delphi.

-Michael