✅ Mutexing List
I have a service that spawns a thread to execute functions, and the list being access inside the spawned thread is inside of another thread. how do i mutex a list between 2 threads?
15 Replies
basically, the spawned threads are going to remove themselves from the list, but i feel there will be a mutex issue removing the items from the list if they finish at odd times
You don't mutex a list, you use a synchronization primitive (such as a mutex) to synchronize the threads
so, basically create a bool that locks the access to removing/adding items from the list
And it's not hard. As an example:
Not a bool, a lock
oooo
i see
A boolean field is not a sufficient synchronization primitive on its own
good information, appreciated. i will give this a go
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock is the reference for
lock
lock statement - synchronize thread access to a shared resource
Use the C# lock statement to ensure that only a single thread exclusively reads or writes a shared resource, blocking all other threads until it completes.
There's a lot more to learn when it comes to synchronization, but this is the basic version
this is a little bit more than i thought it'd be xd
Synchronization always is
then again, the threads i was working with weren't real threads
python 🤢
i'll study this to my needs, thanks
If you don't have any other questions, consider $close ing the thread
Use the
/close
command to mark a forum thread as answeredClosed!