How to execute tasks in parallel in Swift in Swift Package

For some reason, I wanted to install Swift on Ubuntu and try parallel processing, but it didn't work, so I'll show you the problem and the solution.

environment

The environment I ran is as follows.

Thing you want to do

--I want to confirm that processing is executed in parallel for the time being. --Print numbers asynchronously

After investigating, it seems that it is common to use Dispatch queue. It seems that there are also Process and Thread, but Dispatchqueue seems to make them easy to use, so I decided to use Dispatchqueue obediently.

problem

At first, copy the code of Site that came out after searching as it is, and do as follows Was there.

I ran swift package init --type executable to generate various files and made main.swift as follows.

import Foundation

var value: Int = 2

DispatchQueue.main.async {
    for i in 0...3 {
        value = i
        print("\(value) ✴️")
    }
}

for i in 4...6 {
    value = i
    print("\(value) ✡️")
}

DispatchQueue.main.async {
    value = 9
    print(value)
}

However, the execution result is

4 ✡️
5 ✡️
6 ✡️
0 ✴️
1 ✴️
2 ✴️
3 ✴️
9

I wanted to be

4 ✡️
5 ✡️
6 ✡️

It has become. In other words, the processing of the part left to ** Dispatchqueue is not executed at all. ** **

Solution

I had one idea about this cause. It was ** running the file alone **. Many sites are intended for use within iPhone apps developed on the Mac. In addition, I remembered that I had a similar problem before when running Swift as a single file. Below are the questions I posted at that time. [Swift] Timer cannot be executed repeatedly (regular execution) This is a problem because RunLoop, which is automatically executed in the application, is not executed when the Swift file is executed alone, and it is solved by adding RunLoop.current.run () to the end of the program. Did. How did I add RunLoop.current.run () to this program as well?

import Foundation

var value: Int = 2

DispatchQueue.main.async {
    for i in 0...3 {
        value = i
        print("\(value) ✴️")
    }
}

for i in 4...6 {
    value = i
    print("\(value) ✡️")
}

DispatchQueue.main.async {
    value = 9
    print(value)
}

RunLoop.current.run()

Then, the output was as expected.

Summary

--When running Swift as a single file instead of an app, adding RunLoop may solve the problem. --When using Dispatch queue, add Run Loop

Finally

If you have any mistakes or improvements, please leave a comment or edit request. Thank you very much.

Recommended Posts

How to execute tasks in parallel in Swift in Swift Package
[swift5] How to specify color in hexadecimal
[Swift] How to fix Label in UIPickerView
How to overwrite Firebase data in Swift
How to execute multiple commands in docker-compose.yml
How to add sound in the app (swift)
How to implement UICollectionView in Swift with code only
[Java] How to execute tasks on a regular basis
[swift5] How to execute processing when tabBar is tapped
[Swift] How to use UserDefaults
How to use Swift UIScrollView
[Swift5] How to avoid applying dark mode (dark appearance) in code
How to change BackgroundColor etc. of NavigationBar in Swift UI
How to find May'n in XPath
How to hide scrollbars in WebView
How to run JUnit in Eclipse
[Rails] How to write in Japanese
How to run Ant in Gradle
How to learn JAVA in 7 days
How to get parameters in Spark
How to install Bootstrap in Ruby
[Swift] How to use SwiftLint (cocoapods)
How to use InjectorHolder in OpenAM
How to introduce jQuery in Rails 6
How to use classes in Java?
How to name variables in Java
How to set Lombok in Eclipse
[Swift] How to replace multiple strings
How to execute Ruby irb (interactive ruby)
How to concatenate strings in java
How to install Swiper in Rails
[Rails] How to execute "rails db: create" etc. in production environment EC2
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
How to implement search functionality in Rails
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to change app name in rails
How to get date data in Ruby
How to use custom helpers in rails
How to reflect seeds.rb in production environment
How to use named volume in docker-compose.yml
How to filter JUnit Test in Gradle
How to insert a video in Rails
[GCD] Basics of parallel programming in Swift
How to standardize header footer in Thymeleaf
How to include Spring Tool in Eclipse 4.6.3?
How to add jar file in ScalaIDE
How to do base conversion in Java
How to execute with commands of normal development language in Docker development environment
[Architecture design] How to execute SQL in Java application? 2WaySql vs ORM
[Swift] How to use Tab Bar Controller
How to have params in link_to method
How to use Docker in VSCode DevContainer
How to use MySQL in Rails tutorial
How to fix system date in JUnit
[Swift] How to implement the countdown function
How to implement coding conventions in Java
How to embed Janus Graph in Java
[rails] How to configure routing in resources
How to map tsrange type in Hibernate