Forum > General

Simple multithreading code example in Free Pascal

(1/11) > >>

semichaud1:
Hi,

I'm looking for a simple multithreading code example in Free Pascal. How to create threads and use them in the code.

I am currently programming a 2d video game using Free Pascal (v2.6.2.i386-win32) and SDL2 (v2.0.1) and I want to use two threads to process each half of the array I use to display graphics in a grid like fashion on the screen.

Thanks!

ezlage:
Friend,

Take a look: http://wiki.freepascal.org/Multithreaded_Application_Tutorial

semichaud1:
Thanks! I actually went through this tutorial several times but it's doing much more than what I need multithreading for. It seems like the most complicated example of multithreading. I'm looking for somethig simple that I can use for my game to split up the processing of the grid map. If you don't mind could you give me an example of how I would use multithreading in my code to process each half of a grid array[1..100,1..200]?

ezlage:
Try to create a Thread that knows how to do all the work, and through a parameter in the Create procedure you limit the scope of action. Then, you create a Threads vector, initialize each position limited to one part of the grid, and then execute all of them.

I have attached the closest code on your needs I've developed (a dedup ratio calculator). Is a very specific and ancient code.

Sorry by my poor English.


--- 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";}};} ---  TAUThread = class(TThread)    protected      _ID: Int64;      _FileToScan: string;      _FileName: string;      _CurrentHash: string;      _CurrentStart: qword;      _CurrentEnd: qword;      _CurrentSize: qword;      _BlockSize: qword;      _FileSize: qword;      _FileParts: qword;      _ScannedSize: qword;      _ScannedParts: qword;      procedure Execute; override;    private      procedure SendCurrentStatus;      procedure SendStartStatus;      procedure SendEndStatus;    public      constructor Create(const CreateSuspended, AutoFree: boolean; const FileToScan: string; BlockSize: QWord; const ID: Int64=-1);    published  end; {...} var  Form1: TForm1;  Robots: array of TAUThread; {...}  

semichaud1:
Thank you very much! I tried implementing the code but it doesn't work for me. I tried following this tutorial: http://wiki.freepascal.org/Multithreaded_Application_Tutorial#Units_needed_for_a_multi-threaded_application, but I do not understand a lot of the examples of code used in there. Most seem to go beyond what is being explained. I think it's too advanced for me. I have been programming in Pascal for 21 years though. But the code in the tutorial is just confusing. I cannot match it with the explanation. All I want to do is have a thread run half the array for the grid map and another thread run the other half. That's all. But the example in the tutorial has code all over the place with no logical connection between them that I can understand.

Navigation

[0] Message Index

[#] Next page

Go to full version