How can I change a global variable from another th...
# kotlin-native
r
How can I change a global variable from another thread. I tried "@ kotlin.native.SharedImmutable" but this led to the error: "IncorrectDereferenceException: Trying to get it out of the non-main thread "
d
Have you tried an
AtomicReference<String>
?
👌 1
r
No, I just started getting acquainted with writing code, and did not know about it. If not difficult, a brief explanation of how to use it. I will study descriptions on the Internet for now.
d
@kotlin.native.SharedImmutable var lang = AtomicReference("")
.
lang.value = "Thread"
.
That should do it.
r
Same mistake IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLo cal or @SharedImmutable from non-main thread
s
Declare it as
val
instead.
r
Yes it changes the value, thanks. But the flow collapses after 3 passes (this is very similar to the question I asked above), is this a difficult question? Can I count on a hint?
d
Sorry, what do you mean by the flow collapses?
s
Please refer to
GetKeyboardLayoutName
documentation.
r
Is this you pointing to my mistake using the
GetKeyboardLayoutName
function?same C # code works without problems
flow collapses
- it means the program ends. And
try -catch
does not catch errors
d
Try
val Input = allocArray<WCHARVar>(KL_NAMELENGTH)
instead.
And change
Input.ptr
to just
Input
.
Although you'll want to check the return value of the call to
GetKeyboardLayoutName
before calling
toKString()
. https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getkeyboardlayoutnamew
r
Why should I use
allocArray <WCHARVar>
instead of
alloc <WCHARVar>
? In
val Input
, a value equal to" 00000409 "is written, why is an array allocated for it? What phrase in the documentation is understandable?Thanks a lot, it made the code work, but it didn't make me understand.
d
The buffer (of at least KL_NAMELENGTH characters in length) that receives the name of the input locale identifier, including the terminating null character. This will be a copy of the string provided to the LoadKeyboardLayout function, unless layout substitution took place.
It requires a buffer (array of bytes) instead of a pointer to a single character.
r
So buffer = array. This will fix a lot of mistakes on the future, thanks, thank you!)
But still, he writes a value to it once. Then it goes out of the
memScoped
block, and therefore it must be able to write the value a second time, no?
d
It should be able to. I'm not sure why your program stops.
r
So it is possible to write the value in
alloc <WCHARVar>
, did I understand you correctly?
d
No, it writes out a series of values, so an array is required. (Unless KL_NAMELENGTH == 1).
r
and when I allocate
WCHARVar
is it not an array or is it a single cell array? There is an example that shows that you can write a long value
Copy code
int main ()
{
    const wchar_t * s = L "Hello, World!";

    std :: wcout << s << std :: endl;
}
And I get to write the value, but only once.
d
It is a single cell array.
r
Thanks you