Root > Advanced topics > Multi-threaded applications > Creating threads

Creating threads

Previous pageReturn to chapter overviewNext page   

There are the following possibilities to create threads in Delphi / C++ Builder applications:

CreateThread - a basic function to create any thread. This is a system function;
oBeginThread - a wrapper for CreateThread function. This is a RTL function;
TThread - a wrapper for BeginThread function;
Frameworks (AsyncCalls, OmniThreadLibrary, etc.) - wrappers for TThread class;
oThread pools (QueueUserWorkItem, etc.) - wrappers for CreateThread function;


Usually your code will use TThread or BeginThread as low-level thread creation routines. Or your code may use frameworks/thread pools. You should never use CreateThread function to create threads.


Important: as a general rule, you should write your application in a such way that it will behave correctly without EurekaLog on board. This means that if your or 3rd party code throws an exception in a thread - it must be handled in a way that you expect. For example, by showing an error message, canceling action, retrying, etc. Once your application behaves like it is supposed to do - then you add EurekaLog for it. EurekaLog will auto-handle all unhandled exceptions automatically (by hooking few well-known places), and for everything else - you can call EurekaLog from your own handlers when needed. In other words:

Incorrect: your application does not show messages for exceptions in thread and you are adding EurekaLog in hopes to fix this behavior;
Correct: your application correctly handles thread exceptions and you are adding EurekaLog to receive reports about such exceptions.


EurekaLog offers two extended routines which greatly simplify creating threads for debugging:

BeginThreadEx function - a wrapper for BeginThread function which adds support for naming thread and enabling EurekaLog. This function can be used in any application without enabling/including EurekaLog.
TThreadEx class - a wrapper for TThread class which adds support for naming thread and enabling EurekaLog. This class can be used in any application without enabling/including EurekaLog.


It is recommended to use BeginThreadEx function instead of BeginThread function and to use TThreadEx class instead of TThread class.


Important note: turning off low-level hooks means that EurekaLog will not install additional hooks for API functions. This means that EurekaLog will not intercept important system calls. For example, EurekaLog will not hook ExitThread function, which means EurekaLog will not know when a thread exits. This will lead to thread information stored forever - until application terminates. You can call internal _NotifyThreadGone or _CleanupFinishedThreads functions (from EThreadsManager unit) to notify EurekaLog about thread's termination. Such manual notifications can be avoided by using EurekaLog's wrappers (BeginThreadEx and TThreadEx).


Note: when your thread is processing exception, the processing will be performed in a background thread. EurekaLog's event handlers will be called from a background thread. You need to use some sort of synchronization if you are accessing global data in your EurekaLog's event handlers. This also means that several threads can process multiple exceptions simultaneously at the same time. You can avoid this by marshaling processing to main thread. For example, you can use TThread(Ex) and analyze .FatalException property in your main thread. See description of TThreadEx class for examples. Alternatively, you may use "Consecutive processing" option.



See also:

Send feedback... Build date: 2022-03-28
Last edited: 2022-02-15
The documentation team uses the feedback submitted to improve the EurekaLog documentation. We do not use your e-mail address for any other purpose. We will remove your e-mail address from our system after the issue you are reporting has been resolved. While we are working to resolve this issue, we may send you an e-mail message to request more information about your feedback. After the issues have been addressed, we may send you an email message to let you know that your feedback has been addressed.

Permanent link to this article: