[TD71] Threads & critical sections

General discussion forum about all things Team Developer 7.x
Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 484
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

[TD71] Threads & critical sections

Post by Dave Rabelink » 11 Jan 2018, 06:55

To try out more threading features, I started with critical sections.

Using WinApi threading functions we could in fact implement more advanced stuff (like synchronization etc)

Attached is my tryout using

InitializeCriticalSection
EnterCriticalSection
LeaveCriticalSection
DeleteCriticalSection

I do not get it working.
The issue is that for what I see, the critical section object is already in use after InitializeCriticalSection.

Calling TryEnterCriticalSection gives me FALSE as return value indicating another thread already owns the object.
So when calling EnterCriticalSection (from the main thread or from a worker thread), it will wait to get ownership and TD hangs.

Maybe the external function definition is incorrect, but I tried a lot of different options.

So my question is: is it possible to use these thread functions in TD and if so, what the issue is.

The test application has a couple of buttons. The first one is to call InitializeCriticalSection.
The Start thread button will start the thread which will do EnterCriticalSection, LeaveCriticalSection.

CriticalSections_TD71.zip

See that the thread is running but is waiting for the ownership, but should have gotten it immediately because no other call to EnterCriticalSection has been made.
You do not have the required permissions to view the files attached to this post.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 484
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: Threads & critical sections

Post by Dave Rabelink » 12 Jan 2018, 10:03

Marcel Ward has posted the solution for this.
For keepers I will report his response
Marcel Ward wrote: The problem seemed to be with the initialisation of the static structure for the critical section. As per the Microsoft WinApi help for InitializeCriticalSection():

The process is responsible for allocating the memory used by a critical section object, which it can do by declaring a variable of type CRITICAL_SECTION.
Before using a critical section, some thread of the process must initialize the object.


I used SalSetBufferLength() to allocate enough space for the main struct and CStructAllocFarMem() along with CStructPutLong() to set the pointer to a new DebugInfo struct therein.

Also, modified the kernel32 function parameters to take a LPVOID string. (By making it non-Receive, this should ensure that the string buffer pointer remains pointing to the same memory location after each call).
Thread safety for kernel32.dll (which you know about) is also set to Yes.

I added a Sleep() and some reporting so we can see when the background worker thread has entered the critical section and is busy before leaving.

It seems to work well, though I did get one crash whilst I was testing. I haven't worried about the 64 byte memory leak due to calling CStructAllocFarMem(). It does need an accompanying CStructFreeFarMem() when the critical section is no longer needed.
Here is Wards sample:

CriticalSections_TD71_StringBuffer.zip
You do not have the required permissions to view the files attached to this post.
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Dave Rabelink
Founder/Site Admin
Founder/Site Admin
Netherlands
Posts: 484
Joined: 24 Feb 2017, 09:12
Location: Gouda, The Netherlands

Re: [TD71] Threads & critical sections

Post by Dave Rabelink » 30 May 2018, 10:51

BUMP
Regards,
Dave Rabelink

Image
Articles and information on Team Developer Tips & Tricks Wiki
Download samples, documents and resources from TD Sample Vault
Videos on TDWiki YouTube Channel

Return to “General Discussion”

Who is online

Users browsing this forum: Bing [Bot], Ccbot [Crawler] and 0 guests