Forum > General
Cannot run a process procedure in a thread
glubbish:
Ok.
I worked out what the issue was.
it was creating a new thread before the previous thread got going.
So some variables from thread 1 would be those from thread 2, etc.
I put a sleep in after the .start and that seems to work, but is not elegant.
Is there a better way to do this?
cdbc:
Hi
Yup, it's /expensive/ to start a thread ~ 1-2 msecs, depending on how old your gear is... :o
A little sleep here and there, works when starting a thread... Elegant? I dunno.
One solution to this, is to use a 'Thread-pool', full of already started, but sleeping threads. That way, when you get a thread from the pool, startup is instant. *Advanced stuff*.
In your case however, I'd be satisfied with sleep(if it works), because what's 1 msec in your work...?
Regards Benny
glubbish:
Thanks Benny.
Will read up on thread pools and see if I can make sense of it.
At the moment my sleep is 10 seconds. Will see if I can reduce that.
My PC is a AMD Ryzen 5 7600 6-Core Processor with 32 gig of ram. So reasonably newish.
440bx:
--- Quote from: cdbc on July 27, 2024, 12:17:38 pm ---Hi
Yup, it's /expensive/ to start a thread ~ 1-2 msecs, depending on how old your gear is... :o
--- End quote ---
It's expensive but not _that_ expensive.
On a dual core VM running at 2.8Ghz with 5GB of memory it takes an _average_ of 60 secs to create and end 100,000 threads. That means Windows is creating 1,666 threads per second but, this is not a fair measurement, that average is quite a bit worse than the time it takes to create a dozen threads (the typical case), because thread creation and termination occur asynchronously which means that when 100,000 threads are created in a tight loop (with the goal of measuring average elapsed time to create a single thread), the system ends up having to manage _thousands_ of threads that have not ended yet and, those thread consume large amounts of memory (stack allocations.) Also, to keep the system running reasonable well, it is necessary to call CloseHandle and ExitThread for each thread (which consume time not spent creating threads.)
Bottom line, the test indicates that Windows is creating an average of 1,666 threads per second but, in normal circumstances (creating about 16 threads), it is extremely likely that the average is several times that number.
For the curious out there, here is the code used to test CreateThread:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$APPTYPE CONSOLE} program _CreateThreadElapsedTime; uses Windows ; function EmptyThread(Parameter : pointer) : DWORD; stdcall;begin ExitThread(0);end; var ThreadHandle : THANDLE; ThreadId : DWORD; i : int32; const LOOP_COUNT = 100000; begin i := 0; while i < LOOP_COUNT do begin ThreadHandle := CreateThread(nil, 0, @EmptyThread, nil, 0, ThreadId); if ThreadHandle = 0 then begin writeln('CreateThread failed. Loop terminated at ', i); break; end; CloseHandle(ThreadHandle); inc(i); end; if ThreadHandle = 0 then begin { if CreateThread failed wait for a carriage return } readln; end;end.
PS: only tested 64 bit version of the code (32 bit would take longer.)
ETA:
The lack of timing instructions is because my command line interpreter can measure the elapsed time between process creation and termination. Since the CLI does it, I don't need to add code to measure elapsed time.
Navigation
[0] Message Index
[*] Previous page