The use of external threads (mingw) and the procedures GetMem in Lazarus, lazarus / 34-Windows ---------------------------------------------------------------------- SmallTed · 2015-12-07 17:31 Hi I have a .dll that has been compiled in MinGW. Dll has a  thread and sends a callback events. 1. Is through such The callback can use the procedure GetMem. GetMem is ThreadSelf but is for external threads ??. 2. How to synchronize access to shared data. The question asks, because I have not found a detailed description of this problem in lazarus forum. Regards AG ---------------------------------------------------------------------- balazsszekely · 2015-12-07 20:41 Hi SmallTed, I doubt somebody can give you a useful answer, without knowing what the remote thread is doing exactly and what variables are passed with the callback function. Generally speaking no, it will not be thread safe. I would rewrite that dll to lazarus. See this example for a callback function: (http:/ /forum.lazarus.freepascal.org/index.php/topic,30275.msg192494.html#msg 192494) regards, GetMem ---------------------------------------------------------------------- SmallTed · 2015-12-08 08:29 Hi Thanks for the post. Link regarding Exception is most correct. because this issue I wanted to raise. As previously noticed the main problem is the handling Heap and exceptions. On page http://wiki.freepascal.org/Multithreaded_Application_Tutorial This is the chapter "External threads" but the main statement is "avoid external threads". Therefore I would like to hear your experiences on this topic. I add more information about DLLs:   1. Written in C; 2. compiler mingw-64 for win32 3. Exception type sjlj (So I set at compile time, and lib) 4. .dll calls the two functions, one sends text PChar; The second address to the record of the data. 5. .dll uses threads such win32 libraries joined to the compiler. My questions (for windows) 1. What kinds of exceptions, Lazarus uses. (sjlj DWARF ...) 2. What kinds of type threads  POSIX , or Win32 3. What kind of solutions you used in programs. As for your example. I know that this is just an example of exception handling in external DLLs. You have entered an example (the program and dll made Pascal) and uploading in the record type String. Sometimes pascal  converts string and calls additional procedures (see box asemeblera before ShowMessage). I think that example is not thread-safe. SmallTed ---------------------------------------------------------------------- SmallTed · 2015-12-15 07:09 Maybe someone will have a similar problem. We attach DLLs with internal Thread, which refers to our program (Lazarus) through the callback. but in the callback function we can not use: 1. try 2. Do not use appeals to GetMem 3. not to use the facilities Lazarus even those Thread When sending data through the callback, I used a simple method but perhaps the most effective: Prior to joining dll assume Circular buffers for each stream. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i generic TThread_QUEUE_G= record    public      Count,Tail,Head:integer;    ArrayQUEUE:array[0..MaxLengthQUEUE-1] of t;      function IsFull:integer; inline;   {**< if 1 is full        *}      function IsNoEmpty:integer; inline; {**<  if 1 is epty       *}      function Insert(var w:T):integer; {**< if 1 data save         *}      function Get(var W:T):integer; {**< if 1 data is        *}      procedure Clear; {**<  not clear in work        *}      procedure Init;end;      Disappears us a problem installing memory in callback. the synchronization reduces to the use of two procedures:       InterlockedIncrement(Count);        InterLockedDecrement(Count); The creation of a buffer for records on the type of TRecDataS looks like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  QUEUE_1: specialize  TThread_QUEUE_G;  SmallTed ---------------------------------------------------------------------- marcov · 2015-12-15 10:02 Try calling pascal "initthread" (on startup) and "donethread"  (on shutdown) in your external thread to initialize the heapmanagers threadvariables. These are defined in system (see inc/threadh.inc). ---------------------------------------------------------------------- SmallTed · 2015-12-15 12:28 Marcov Describe (it can be useful to others) my fight after the first post in the forum. 1. At the first rehearsal, my mistake was that I did not use threads in the program. It was necessary to create at least one, and even end it. Variable IsMultiThread will = true. 2. Then, as I understand it (correct me) whenever the callback must initiate heap for the thread by: ThreadManager.AllocateThreadVars; InitThread (1024); But if I need to clean ? If I must call this function every times..... I decided that I will not rule over it, and at the expense of integration with Lazarus, apply the previously reported method, which has been running for a few days. According to me (correct me) if callback do not use the appeals Lazarus procedures  (Heap) and do not exceed the stack ,which dll declared for the thread. Everything should go. Marcow thanks for the info brief, but useful. If you have the time may explain the exact problem. SmallTed