You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.P3A lower priority bug or feature requesttriagedIssue has been triaged by sub team
import'dart:isolate';
voidmain() {
Future.wait([
for (var i =0; i <100; i +=1)
Isolate.run(() {
print('test');
})
]);
}
The expected output is 100 lines that each contains a single test, but the actual result is that some of the lines are empty and some of the lines will have testtest.
The underneath issue is that print uses writeln internally, and writeln would output the message and a newline character in two separate calls, causing a race condition between threads.
As a workaround, directly calling write with newline character added to message is thread-safe.
import'dart:isolate';
import'dart:io';
voidmain() {
Future.wait([
for (var i =0; i <100; i +=1)
Isolate.run(() {
stdout.write('test\n');
})
]);
}
The text was updated successfully, but these errors were encountered:
ntkme
changed the title
print is not thread-safeprint and writeln are not thread-safe
Sep 9, 2023
Adding the \n on the string will copy the entire string one extra time. That's definitely unnecessary overhead.
(And on Windows, we'd still have to convert the \n to \r\n afterwards.)
One thing we could perhaps do, is to let the internal implementation of print include the line terminator in the same native memory buffer that we convert the string to UTF-8 into anyway. Then we can emit the line terminator along with the string contents, in the same native write call.
If we give Dart_StringToUTF8 an extra integer argument, the number of extra bytes to make room for in the allocation, after the bytes of the string, then we can insert the newline into that, instead writing it as a second call.
area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.P3A lower priority bug or feature requesttriagedIssue has been triaged by sub team
Here is the minimum reproduction:
The expected output is 100 lines that each contains a single
test
, but the actual result is that some of the lines are empty and some of the lines will havetesttest
.The underneath issue is that
print
useswriteln
internally, andwriteln
would output the message and a newline character in two separate calls, causing a race condition between threads.As a workaround, directly calling
write
with newline character added to message is thread-safe.The text was updated successfully, but these errors were encountered: