tthread | TinyThread fork with lambda , future and continuation | Reactive Programming library
kandi X-RAY | tthread Summary
kandi X-RAY | tthread Summary
TinyThread (tthread, is a lightweight and portable C threading library. This project extends TinyThread++ by providing.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of tthread
tthread Key Features
tthread Examples and Code Snippets
Community Discussions
Trending Discussions on tthread
QUESTION
Considering the following SO Question: Is it dangerous to use synchronize in a non-VCL application?
I was wondering how TThread's Synchronize
, CheckSynchronize
and WaitFor
is affected when using them inside an Apache Web Application DLL.
I started investigating this when I realized WaitFor
was locking/hanging. Inside WaitFor
it checks if CurrentThread.ThreadID = MainThreadID
and then repeats checking the result of MsgWaitForMultipleObjects
and based on the result it will do CheckSynchronize
or PeekMessage
and when it received a WAIT_OBJECT_0
it will finally exit the loop. If the MainThreadID
is not the same as the CurrentThreadID
, then WaitFor
will do a single WaitForSingleObject
.
So I created the following test Apache Web Module DLL to see what the MainThreadID
is during the lifetime of a web request.
ANSWER
Answered 2021-May-11 at 11:02To recap:
A DLL's DLLMain/DLLProc
is called whenever a process is "attached/detached" and a thread is "attached/detached"
There is also a blog post by Raymond Chen that discusses thread deadlocks in DLLMain
.
Because DLLMain
is called when the the DLL is unloaded and also when a thread exits I cannot do a thread WaitFor
in the DLL Unloading code (OnWebModuleDestroy
) as that will obviously cause a deadlock as the one will be waiting for the other.
After R.Hoek's comment I started looking to see if Apache calls some sort of cleanup procedure before it actually unloads the module DLL and came upon its apr_pool_cleanup
and the companion apr_pool_cleanup_register
procedure which registers a hook into the the cleanup. Fortunately this has already been handled inside the Web.ApacheApp
unit and its TApacheApplication
class that has an OnTerminate
"event" which is called by the apr_pool_cleanup
.
Assigning cleanup code to the OnTerminate
"event" allows you to properly get rid of your threads without fear of deadlocks in DLLMain
:
QUESTION
I'm using Delphi XE7 on Windows 10.
I have been using the following code for a long time, and just read the documentation on SetTimer()
. To state it simply, I am setting timers from non-UI threads, but Microsoft's documentation says they should only be set on the UI thread. Extensive tests show my code works fine, but I can't trust my system to behave the same as other systems, or the Microsoft documentation to be 100% accurate. Can anyone verify whether this code is OK or not OK?
The Delphi code will not deadlock, it pretty much just calls SetTimer()
(I am aware there is a race condition setting TTimer.FEnabled
).
The MSDN documentation says:
hWnd
Type: HWND
A handle to the window to be associated with the timer. This window must be owned by the calling thread.
What I'm trying to accomplish is worker threads doing stuff, and when appropriate, they notify the main thread that elements of the UI must be updated, and the main thread updates the UI. I know how to use TThread.Synchronize()
, but deadlocks can happen in certain cases. I can use PostMessage()
from my worker threads and handle the message in the UI thread.
Is there any other way in Delphi to notify and update the UI thread?
...ANSWER
Answered 2021-May-07 at 17:01The TTimer
is being constructed in the main UI thread, when the TForm
streams in its DFM resource. The TTimer
's constructor creates an internal HWND
for the timer to receive WM_TIMER
messages with. That HWND
is thus owned by the main UI thread.
TForm.Notify()
is setting the timer's Enabled
property to true
, which will call SetTimer()
. Notify()
is being called in the context of the worker thread, not the main UI thread. This SHOULD NOT work, as stated in SetTimer()
's documentation. Only the main UI thread should be able to start the timer running, since the main UI thread owns the timer's HWND
.
TTimer.UpdateTimer()
, which is called internally by the setters of the timer's Enabled
, Interval
and OnTimer
properties, will raise an EOutOfResources
exception if SetTimer()
fails. So, calling form1.Notify()
in TypeThreadTest.Execute()
SHOULD NOT work. The only way SetTimer()
would not be called in that situation is if either:
Interval
is 0Enabled
isfalse
OnTimer
is unassigned
Otherwise, your worker thread SHOULD be crashing.
As you have noted, your worker thread can alternatively use TThread.Synchronize()
(or TThread.Queue()
), or PostMessage()
(or SendMessage()
), when it wants to notify the main UI thread to do something. These are viable and preferred solutions. Personally, I would opt for TThread.Queue()
, eg:
QUESTION
Hi I have a multidevice APP based on FMX.
The synch database process takes longer time, I need a AniIndicator1 enabled to tell user to wait.
Following code tested in n android phone, sometime works, sometime only finish the first synch DB function, sometimes finish first 3 and then exited. Sometimes none of the sychDB function was taken place.
...ANSWER
Answered 2021-Mar-28 at 04:48You can't directly access UI controls from within a worker thread, like you are doing. You MUST synchronize that access to the main UI thread.
QUESTION
Hello all,
i am having problems getting the file preview (the one shown on the right side in the Windows Explorer window) for a certain file.
So far fetching the file preview works fine, but it takes a long time (between 0.5 and 2 seconds). Thus i do not want it to be executed in the main thread (as this would interrupt the program gui).
I tried to execute the file preview extraction in a worker thread, but this yields a SIGSEGV
.
The call stack is also not really useful, it only shows that the exception is raised in ShellObjHelper
in Line 141 (see source code below).
Source Code for main unit:
...ANSWER
Answered 2021-Apr-13 at 07:07Thanks to the comment from @IInspectable, that's the hint i needed.
Solution:
Add CoInitialize
before calling GetExtractImageItfPtr
and add CoUninitialize
after receiving the file preview, but still within the worker thread.
Ensure that CoUninitialize
is called even if exceptions occur by using try
and finally`.
Working source code for main unit with worker thread:
QUESTION
I created two WndProcs in the main thread and then I post a messages from other threads to each of them, almost at the same time, but starting with WndProc1. This WndProc1 has a job to do which lasts for some time... It sigals when it begins and when it ends. WndProc2 also signal when is accessed. Now, when I press the button to start this test, I get: "P1-Enter ... [delay] ... P1-Leave WndProc2". As you can see, the second message waits for WndProc1 to finish, although he was sent to to WndProc2 ! What I want to know is how this serialization works if those two WndProcs have nothing in common ? I think that even happens if I have two different components, each with his own WndProc (but I didn't check).
...ANSWER
Answered 2021-Mar-21 at 09:18What you see is perfectly expected. Each thread has one and only one message queue and may have zero to many window handles. Window handles usually correspond to visual components but not necessarily as in your example.
Somewhere in the code (for a GUI in Delphi, this is in the Forms unit), there is a so called "message loop" which retrieves messages from the queue and dispatches them to the corresponding WndProc
. The dispatch mechanism is like a simple function call: it is blocking while a message is being processed, unless the message handler calls the message pump again (where reentrancy problems start, if not handled correctly). Look at the documentation and look in the Forms unit for TApplication.ProcessMessages
and in the Classes unit for AllocateHWnd
/DeallocateHWnd
.
If you want to execute code in parallel you must create separate threads; each one will execute in parallel as long as there are fewer threads than CPU cores and threads are not blocked by I/O or mutexes, semaphores and critical sections. If too many threads are ready to execute they are scheduled using preemptive multitasking.
You can communicate between threads using messages. For that, a thread must create a window handle and have a message pump.
In Delphi, the GUI can only be accessed from the main thread. If a secondary thread has something to display then it must let the display code execute by the main thread, most likely again through a message between the secondary/worker thread and main thread, or use the Synchronize
method, or use other communication mechanisms such as pipes, sockets, file I/O, shared memory. Alone or in combination.
QUESTION
Following the tips from this Stack Overflow answer I created a simple application for Windows that can get a snapshot from the webcam, using DirectX library.
Now I am trying to get the same result using thread
. Here is what I got so far:
ANSWER
Answered 2021-Mar-13 at 19:01DirectX uses ActiveX/COM interfaces. As such, your thread's Execute()
method needs to initialize the COM library for itself via CoInitialize/Ex()
before accessing any COM objects.
But more importantly, you are creating and using the TVideoImage
object across thread boundaries. Most COM objects are not designed to be used across thread boundaries, they would have to be marshaled in order to do that. So don't use TVideoImage
that way. Create, use, and destroy it all within the same thread (ie, inside your Execute()
method).
Try this instead:
QUESTION
Background
Using the TThread.CreateANonymousThread(aProc:TProc)
I can create a thread that destroys the thread object after the thread has terminated. (or alternatively by setting FreeOnTerminate
to true
for athread object). This allws the thread initiator routine th finish and go out of scope, while the tread keeps on running. (That's what I am looking for)
ANSWER
Answered 2021-Mar-01 at 15:10The answer is simple: You don't have to do anything special. The ITask
interface returned by the TTask.Create
call is also "held onto" internally by the InternalExecute
method, so the underying TTask
object will be destroyed by means of reference counting. If the "Master" thread does not hold on to the ITask interface, the subthread will. Until it has terminated.
So using TTask
this way is pretty straightforward.
NOTE: In RS10.4.2 this works, I suspect using captured interface variables may cause a problem in 10.4.1 and earlier due to inline var problems combined with anonymous procs. (Didn't try)
QUESTION
I have a Delphi 2010 application that exports a DLL and has the library
header. It creates its MainForm in a TThread, like so:
ANSWER
Answered 2021-Feb-17 at 23:33When compiling this code as a program
, at runtime it will try to terminate itself when end.
is reached, before the worker thread even has a chance to run, which could possibly (and likely) happen after the Application
object has been destroyed. You would have to wait for the worker thread to finish its work before letting the program exit, eg:
QUESTION
The Delphi help for TThread.OnTerminate
states that:
The method assigned to the OnTerminate event is executed in the context of the main thread rather than the context of the thread being terminated.
Is this even the case when the thread is created in another thread than the main thread?
So, is the OnTerminate
called in the thread that created the TThread
, or is it called in main thread? The IDE does not tells me this. When I debug, I see no active thread in the OnTerminate
event. :-/
ANSWER
Answered 2021-Jan-20 at 19:16The documentation is correct. The OnTerminate
event handler is always run in the main thread by default. Internally, TThread.DoTerminate()
(which is called after the thread's Execute()
method exits) uses TThread.Synchronize()
to call the handler:
QUESTION
I've got 5 labels on my form whose Fonts styles should all get the BOLD property added. I tried doing this using the code below but only Label2 gets bold and the rest still stays as it is at design time.
...ANSWER
Answered 2021-Jan-15 at 20:49For all labels, for which you want to manipulate the Style
property, you need to remove the Style
setting from StyledSettings
.
(As the color change works, you already removed the FontColor
setting from StyledSettings
)
Actually, if you set any of these settings at design time, the IDE removes the corresponding StyledSetting
. If you don't change any of these settings at design time, you must remove the corresponding StyledSettings
before you can change the setting at runtime.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install tthread
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page