Yeah you are right, Windows was less forgiving. Sorry about that and thank you Jean.

For simple data structures written only from one thread, using critical section may be overkill. But if your data structure is a queue or a longer buffer, with length, etc. that is being produced by one thread and consumed by another (i.e., modified by more than one thread) then you should use some form of synchronization such as a critical section.


On 8/28/19 7:26 AM, James Richters wrote:
Thanks for figuring out the critical section needs to be initialized.  Stefan's 
example did not do this:
https://github.com/svpantazi/libusbxhid/blob/master/libusbhid_test_with_thread.pp

maybe it's something you can get away with on Linux...

I'll put in the init and done.   Should I enter critical section only for 
writing to shared variables, or also when reading them?

I'm also wondering if there is some way to tell if a thread is currently 
running or not... I don't want to try to start it again if it's already 
running.  I can make some flag that the threat sets when it starts and clears 
when it exits, but I am wondering if there already something in place to check 
to see if a thread exists.

James


-----Original Message-----
From: fpc-pascal <fpc-pascal-boun...@lists.freepascal.org> On Behalf Of Jean 
SUZINEAU
Sent: Wednesday, August 28, 2019 7:04 AM
To: fpc-pascal@lists.freepascal.org
Subject: Re: [fpc-pascal] USB Human Interface Devices

Hello,

It seems you didn't initialized you critical section using InitCriticalSection.
The documentation of EnterCriticalSection :
https://www.freepascal.org/docs-html/rtl/system/entercriticalsection.html
The one of InitCriticalSection:
https://www.freepascal.org/docs-html/rtl/system/initcriticalsection.html

At the end, you need to call DoneCriticalSection  to release the associated 
system resources ( 
https://www.freepascal.org/docs-html/rtl/system/donecriticalsection.html).

Note: these is related to freepascal implementation of critical sections, in 
windows API, the function names are slightly different, 
InitializeCriticalSection/EnterCriticalSection/DeleteCriticalSection
(https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-entercriticalsection)


Le 28/08/2019 à 01:50, James Richters a écrit :
One thing I wasn't able to duplicate however was the use of      
EnterCriticalsection(criticalSection);  and  
LeaveCriticalsection(criticalSection);  when writing to shared variables.  If I 
try to ever use EnterCriticalsection(criticalSection); in the read thread, My 
program just instantly locks up and I can't even break out of it.    If I try 
to use it in the main program I instantly get
EAccessViolation: Access violation
    $00007FFF18A2DF23
    $00007FFF189E9BBC
    $00007FFF189E9AD0
    $000000010000DCDA
    $000000010000D54B
    $000000010000218B  PROCESS_USB_DATA,  line 475 of WHB04B-4_test.pas
    $0000000100002B37  SIMPLETERMINAL,  line 641 of WHB04B-4_test.pas
    $0000000100002DDD  USE_MPG_DEVICE,  line 675 of WHB04B-4_test.pas
    $0000000100002F93  main,  line 699 of WHB04B-4_test.pas
    $0000000100002FE6
    $0000000100011350
    $0000000100001980
    $00007FFF17B47E94
    $00007FFF18A4A251
Line 475 is    EnterCriticalsection(criticalSection);
I left where I had the criticalsection stuff in the program but commented out.  
 It does seem to work fine without it though.. since the read, I am curious 
what I'm doing wrong, or if I need to do something else because I'm on Windows.

_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org 
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


--
_______________________________________________________
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to