kurt_steiner
01/20/2024, 3:00 PMimport kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.joinAll
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val channel1 = Channel<Int>(0)
val channel2 = Channel<Int>(0)
val channel3 = Channel<Int>(0)
val n = 5
val job1 = launch {
for (i in 1..n) {
channel1.receive()
print("0")
if (i % 2 == 0) {
channel2.send(1)
} else {
channel3.send(1)
}
}
channel2.send(1)
channel3.send(1)
}
val job2 = launch {
for (i in 2..n step 2) {
channel2.receive()
print(i)
channel1.send(1)
}
}
val job3 = launch {
for (i in 1..n step 2) {
channel3.receive()
print(i)
channel1.send(1)
}
}
channel1.send(1)
joinAll(job1, job2, job3)
}
ephemient
01/20/2024, 5:52 PMChannel(capacity = 1)
ephemient
01/20/2024, 5:53 PMkurt_steiner
01/21/2024, 4:00 AMfn main() {
let n = 5;
let (tx1, rx1) = crossbeam_channel::bounded(0);
let (tx2, rx2) = crossbeam_channel::bounded(0);
let (tx3, rx3) = crossbeam_channel::bounded(0);
let job1: JoinHandle<anyhow::Result<()>> = thread::spawn(move || {
for i in 1..=n {
rx1.recv()?;
print!("0");
if i % 2 == 0 {
tx2.send(1)?;
} else {
tx3.send(1)?;
}
}
Ok(())
});
let tx1_1 = tx1.clone();
let job2: JoinHandle<anyhow::Result<()>> = thread::spawn(move || {
for i in (2..=n).step_by(2) {
rx2.recv()?;
print!("{}", i);
tx1_1.send(1)?;
}
Ok(())
});
let tx1_2 = tx1.clone();
let job3: JoinHandle<anyhow::Result<()>> = thread::spawn(move || {
for i in (1..=n).step_by(2) {
rx3.recv()?;
print!("{}", i);
tx1_2.send(1)?;
}
Ok(())
});
tx1.send(1).unwrap();
job1.join();
job2.join();
job3.join();
}
tachikema kun ?ephemient
01/21/2024, 9:35 AMephemient
01/21/2024, 9:36 AM?
with .unwrap()
and you'll see that it panicskurt_steiner
01/21/2024, 10:27 AM