Now i’m try to learn multithreading and mutext. But i don’t understand it. For example i add items to list in anotyher thread then main programm:
GMutext* lock;
g_mutex_lock (lock);
g_list_prepend(list, "Some data");
g_mutex_unlock (lock);
What happens in this case with the list? The list of added elements, as well as no access from the main thread until the g_mutex_unlock? Or i wrong understand it?
Thank you.
Thank you.
While the mutex is locked any other thread that wants to lock it will block until the mutex is unlocked by the thread currently holding the lock. This doesn’t relate to the list object – if some other thread doesn’t try to lock the same mutex is can try to do anything with the list and concurrent access might occur causing list corruption.
So locking the mutext is a convention. The caller must obey the convention – try to acquire the mutex lock before accessing the guarded data object. Mapping between data objects and mutexes is up to the developer here.