Reputation: 75
I've been working on the parser for a programming language that requires multithreading support. While investigating what the backend of my compiler should be, I noticed that I cannot find much information on multithreading for things like CIL, LLVM IR, gcc RTL, or JVM bytecode. I can find some references on how to make such code thread safe, but nothing on how to, say, create or fork threads. I can of course use signals or something to interface directly with the operating system, but that's nonportable and error-prone.
Is it the case that there's simply no portable way for managing threads in these low-level languages? Should I compile to a high(er)-level language like C instead?
Upvotes: 3
Views: 648
Reputation: 25757
In JVM byte code you can use any Java libraries, including those that work with threads. The conventional way of creating a thread would be
new Thread() {
@Override
public void run() {
/// code
}
}.start();
This code is written in Java. The corresponding JVM byte code can be seen by using javap:
0: new #2 // class Main$1
3: dup
4: invokespecial #3 // Method Main$1."<init>":()V
7: invokevirtual #4 // Method Main$1.start:()V
10: return
And Main$1 is a class:
final class Main$1 extends java/lang/Thread {
// compiled from: Intf.java
OUTERCLASS Main main ([Ljava/lang/String;)V
// access flags 0x8
static INNERCLASS Main$1 null null
// access flags 0x0
<init>()V
L0
LINENUMBER 7 L0
ALOAD 0
INVOKESPECIAL java/lang/Thread.<init> ()V
RETURN
L1
LOCALVARIABLE this LMain$1; L0 L1 0
MAXSTACK = 1
MAXLOCALS = 1
// access flags 0x1
public run()V
L0
LINENUMBER 11 L0
RETURN
L1
LOCALVARIABLE this LMain$1; L0 L1 0
MAXSTACK = 0
MAXLOCALS = 1
}
This code is perfectly portable.
Upvotes: 2