Concurrent programming method using the Go language in Delphi

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Google's go language, in addition to its cool syntax, is more appealing to developers like Coroutine's Goroutine, who feel it is smarter than Lua's coroutine because of its ability to automatically assemble threads and fibers at run time. In addition, the go language believes that data between threads should be communicated through the channel, not by address. These features are very convenient to develop stable concurrent programs, but also provide a clear idea of concurrent programming.

Go's channel carries a data, used to communicate between multiple coroutine, it is easy to control, because its rules are simple: no value to write, otherwise suspend, have value to read, otherwise also suspend, and read and write is paired operation.

In fact, the idea of go can also be used in Delphi, can be used to imitate the thread Goroutine,delphi mainly for client development, so there is no go efficiency can also be accepted. Here is a demo of the actual effect, hopefully it will give the Delphi programmer in multi-threading another idea.


{$APPTYPE console}uses sysutils, Coroutineunit;begin Tproc (procedure () var c:cchannel<integer>; Declares a channel that can carry an integral type of data. I:integer; Begin C:=cchannel<integer>. Create; For I:=1 to 5 do//create five task threads and start working immediately. Go (//In the Go language, there is a go keyword to run a function in the Goroutine mode, which uses the Go language style, with a go function instead. procedure () Begin SLEEP (1000); Assuming this task is complicated, it takes a second. C.value:=1; After the task is completed, give the channel a value, how much of this value is not important in this example, it just gives a signal: I finished the end); For I:=1 to 5 does//for these five tasks to determine whether the C.value has been completed; When value is valued, this sentence can be read, otherwise wait for writeln (' complete '); C.free; End) (); Readln;end.

Demo Project code and referenced unit in this post

Http://bbs.cnpack.org/viewthread.php?tid=9514&extra=page%3D1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.