Thread uses procedure and variables from interface of another unit. lazarus / 13-General ---------------------------------------------------------------------- anna · 2013-02-20 18:19 So I have multithreaded application. In Execute method there is calling of some procedure from another unit and of course is not private of the thread. Can it cause a conflict? Actually I have an Aho–Corasick search algo and it generate stupid SIGSEG error... But some times application work without error. I can't catch it. ---------------------------------------------------------------------- JuhaManninen · 2013-02-20 20:37 Quote from: anna on February 20, 2013, 07:19:05 pmSo I have multithreaded application. In Execute method there is calling of some procedure from another unit and of course is not private of the thread. Can it cause a conflict? Problems can happen if many threads call the same code at the same time. Also, if the code updates GUI, it must be called from main thread only, using TThread.Synchronize if needed. Juha ---------------------------------------------------------------------- anna · 2013-02-21 03:10 Thank you very much for answer. Can you lell two more things? If I make some procedure protected, but this proc calls another procedure , which is not protected, will it cause conflict? And what happen if I declare a variable inside protected procedure (between procedure & begin), but this variable is not private of the thread? ---------------------------------------------------------------------- taazz · 2013-02-21 03:19 You do not protect procedures/code you protect variables/data, if a procedure does not access only local variables then you can assume it is thread safe. if it does access non local variables - data then those need to be protected. ---------------------------------------------------------------------- anna · 2013-02-21 04:35 Quote from: taazz on February 21, 2013, 04:19:55 amYou do not protect procedures/code you protect variables/data, if a procedure does not access only local variables then you can assume it is thread safe. if it does access non local variables - data then those need to be protected. Threads call aho-corasick search procedures, and these procedures allocate memory. So is it impossible to protect memory? I think that if I put some code to procedure it must protect itself. Otherwise it's no sence in procedures... ---------------------------------------------------------------------- tr_escape · 2013-02-21 06:05 hello could you share your sample codes or part of your codes in this topic? ---------------------------------------------------------------------- anna · 2013-02-21 15:05 Quote from: tr_escape on February 21, 2013, 07:05:24 amhello could you share your sample codes or part of your codes in this topic? I have put another unit to private section of thread declaration completely. Now it seems project work without  obscure SIGSEG error (even abbreviation is obscure). But now I get SIGSEG when I close application. If you need an Aho-Corasick algo , I will better make another topic. ---------------------------------------------------------------------- tr_escape · 2013-02-21 15:30 Quote from: anna on February 21, 2013, 04:05:23 pmIf you need an Aho- Corasick algo , I will better make another topic. Maybe your threads can't free when you close your software. I think they in a progressing a operations. ---------------------------------------------------------------------- taazz · 2013-02-21 15:55 Quote from: anna on February 21, 2013, 05:35:10 amQuote from: taazz on February 21, 2013, 04:19:55 amYou do not protect procedures/code you protect variables/data, if a procedure does not access only local variables then you can assume it is thread safe. if it does access non local variables - data then those need to be protected. Threads call aho-corasick search procedures, and these procedures allocate memory. So is it impossible to protect memory? I think that if I put some code to procedure it must protect itself. Otherwise it's no sence in procedures... Ok my last message is a bit incorrectly worded, it doesn't matter if your procedures allocate memory and pass it on to other procedures etc as long as the memory allocated is not in a global variable or accessed by multiple threads, that memory is considered local. If it is in global variables then those variables need to be protected using any synchronization object you see fit (MultiReadSingleWrite, CriticalSection etc) and use the same object from all the threads that access it. The idea is that each thread that needs access to the data requests permission and if no one is currently using them it gets its permission and goes on processing when finished it flags the end of its processing and moves on, something along the lines of EnterCriticalSection and LeaveCriticalSection calls just make sure that all threads use the same critical section to access the data and do not each create one for them self.