C
C#2y ago
Boggo

❔ Cross Thread synchronization of locks

In over my head, I am trying to use a lock to pause some code on one thread, and i want to release (Monitor.Exit()) from another thread. How do i synchronise this?
71 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
im not aware of how to synchronise because i am exiting from another thread that the lock was entered on, its throwing up an error
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
Boggo
BoggoOP2y ago
so this runs on a thread the cw will be taken out, its just for testing purposes then i call _halter.Exit(); from another Thread
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
queue
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
the idea of the locks was that it wasnt gonna infinitely run a while true loop
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
how does channel work?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
see $channel
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2y ago
Exploring System.Threading.Channels
A blog about software engineering and applied cryptography.
.NET Blog
An Introduction to System.Threading.Channels - .NET Blog
“Producer/consumer” problems are everywhere, in all facets of our lives. A line cook at a fast food restaurant, slicing tomatoes that are handed off to another cook to assemble a burger, which is handed off to a register worker to fulfill your order,
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i would probably agree. however
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
in a hypothetical situatuation where i needed to use a lock and queue since this is for school xD
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
is it possible like how im trying?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i should probably read up a little on locks not sure i understand this
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
private static void StoreMessages()
{
while (true)
{
if (_messagesToStore.Count == 0)
{
lock (_halter)
{
}
}

var msg = _messagesToStore.Dequeue();
Console.WriteLine("Message Uploaded");
_db.AddNewTextMessage(msg);

}

}
private static void StoreMessages()
{
while (true)
{
if (_messagesToStore.Count == 0)
{
lock (_halter)
{
}
}

var msg = _messagesToStore.Dequeue();
Console.WriteLine("Message Uploaded");
_db.AddNewTextMessage(msg);

}

}
i have changed now trying to edit myself
private static void StoreMessages()
{
while (true)
{
Monitor.Enter(_halter);
try
{
if (_messagesToStore.Count == 0)
{
Monitor.Wait(_halter);
}

var msg = _messagesToStore.Dequeue();
_db.AddNewTextMessage(msg);
}
finally
{
Monitor.Exit(_halter);
}
}

}
private static void StoreMessages()
{
while (true)
{
Monitor.Enter(_halter);
try
{
if (_messagesToStore.Count == 0)
{
Monitor.Wait(_halter);
}

var msg = _messagesToStore.Dequeue();
_db.AddNewTextMessage(msg);
}
finally
{
Monitor.Exit(_halter);
}
}

}
heres original it holds it until i exit it from the other thread
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
someone else mentioned reset events too, i had a look but im pressed on time for this so looking for something simple as possible
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
no clue, its my coursework its due friday. Teacher suddenly said he wants it done haha
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i need to finish the solution. Then complete testing and evaluation for my school, the computer science course requires we complete a full project Analysis, design, implementation, testing and evaluation
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
not sure, but i would be using months to measure not years
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i have played about with it for maybe 1.5 years, but for the first year i was playing with strings and arrays
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
ok this is manual?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
yes this one, so i use either or, or do i need both
Boggo
BoggoOP2y ago
AutoResetEvent Class (System.Threading)
Represents a thread synchronization event that, when signaled, resets automatically after releasing a single waiting thread. This class cannot be inherited.
Boggo
BoggoOP2y ago
i am reading here
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
so theres not a huge difference a bit like having a using statement rather that calling .Close() ?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
oh, forget my question then haha
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
so i need manual i believe
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i may have 10+ threads all adding to queue
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
ok i see. I will look at channel again, hopefully i get my head around it pretty quick
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
ok sure, ill try plug it in and see what happens. But not gonna spend too long if it doesnt go well
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
yes, will try this first
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i know a little about this i used to use await Task.Run(() => SomeAsyncTask)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
i know haha it was very bodge job code
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
but i said, i am still new and learning. This is my first proper project
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
oke doke appreciate your time fingers crossed all goes well So i was being dumb i fixed using Locks and monitor i just never synchronised the two threads by entering on both only entered on one thread where i waited from pule i need to enter then pulse then exit i just pulsed
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Boggo
BoggoOP2y ago
im aware, my bad
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mayor McCheese
Channel ftw
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server