Forum > General
Simple multithreading code example in Free Pascal
lainz:
There is a good component to run tasks in separate threads.
https://github.com/wadman/wthread/
reinerr:
Have attached a sample app which processes a random "grid" (it actually does random sleeps). In the app you can hit the button as many times as you want and the threads will go off and do their "processing" and return whenever they are finished.
Note, there is a thread-safe Progress property that could be used to check the progress of a thread from elsewhere. In the example I remember the last thread that was started and display its progress (while it exists).
-Reiner
Leledumbo:
As simple as it can be but not simpler:
--- 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";}};} ---{$mode objfpc} uses {$ifdef unix} cthreads, {$endif} Classes; type TMyThread = class(TThread) procedure Execute; override; end; procedure TMyThread.Execute;begin WriteLn('I am thread with ID ',ThreadID);end; var t1,t2,t3: TThread;begin // create all threads in suspended state t1 := TMyThread.Create(true); t1.FreeOnTerminate := true; t2 := TMyThread.Create(true); t2.FreeOnTerminate := true; t3 := TMyThread.Create(true); t3.FreeOnTerminate := true; t1.Start; t2.Start; t3.Start; // start them all t1.WaitFor; t2.WaitFor; t3.WaitFor; // wait for them all to finish before exiting main threadend.
semichaud1:
Perfect. Thank you all. I'm going to try out those codes.
semichaud1:
So I managed to get multithreading working but it turns out that I cannot use commands relating to graphics in anything other than the main thread which defeats what I wanted to use multithreading for (have one thread render half the grid and another the other half). So I moved the code for calculations, such as character positions and movement, to an additional thread and left everything else including graphics in the main thread. I made sure that all variables I'm using in the additional thread are not used at all in the main thread. My characters on screen are shaking when moving and I think it's due to the calculations and graphics not being synced. So basically the graphics still depend on player position calculation, which is normal. I can't think of a way to make this independent. In order to draw the graphics I need to know the position of each character. When I move characters around the additional thread processes their movements and new positions, the main thread renders the characters on screen and those two things happened asynchronously due to multithreading. I tried inserting a conditional statement to only render characters once the additional thread doing calculations is done but that did not solve the issue.
Navigation
[0] Message Index
[#] Next page
[*] Previous page