Thread and mutex


#1

Hello, I am trying to use mutex in my thread but it does not work because the function lock need an other suspend function and I cannot do that in my thread. Do you have an idea to resolve the problem ?
this is my code :

import kotlin.concurrent.thread
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.sync.Mutex

val t1 = thread(start = false, priority = 5)
{
mut.lock()
for(i in 0…10){
i++
}
println(i)
mut.unlock()
}

val t2 = thread(start = false, priority = 5)
{
mut.lock()
for(i in 0…10){
i–
}
println(i)
mut.unlock()
}

var mut = Mutex()
var i :Int = 0

fun main(args: Array) {
t1.start()
t2.start()
t1.join()
t2.join()
}

thanks for your help


#2

Can you use a lock?

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html


#3

it works thanks !