Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di MrX87
    Registrato dal
    Jun 2007
    Messaggi
    500

    [C] lock/unlock mutex POSIX linux

    Ciao a tutti ragazzi...ho una domanda relativamente semplice.....programmando su linux...sto usando le librerie pthread.h che forniscono delle funzioni per la gestione dei mutex(semafori binari).

    La domanda è: un mutex può essere gestito da più threads?
    Ovvero se faccio la lock() nel thread1 posso fare la unlock dello stesso mutex nel thread2???

    Da quello che so di teoria non si potrebbe fare....perchè un mutex se bloccato dal thread1 deve essere sbloccato dal thread1 stesso!!! però volevo trovare su internet un manuale o qualcosa di ufficiale che spiega questo...perchè devo scriverlo in una relazione e devo dare un riferimento ufficiale.

    Grazie mille
    "Non può piovere per sempre" Il Corvo
    Forza Vigor!

  2. #2
    È tutto spiegato nella manpage di pthread_mutex_unlock... Sostanzialmente, dipende dal tipo di mutex:
    If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection shall not be provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, undefined behavior results.

    If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking shall be provided. If a thread attempts to relock a mutex that it has already locked, an error shall be returned. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error shall be returned.

    If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex shall maintain the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count shall be set to one. Every time a thread relocks this mutex, the lock count shall be incremented by one. Each time the thread unlocks the mutex, the lock count shall be decremented by one. When the lock count reaches zero, the mutex shall become available for other threads to acquire. If a thread attempts to unlock a mutex that it has not locked or a mutex which is unlocked, an error shall be returned.

    If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in undefined behavior. Attempting to unlock the mutex if it was not locked by the calling thread results in undefined behavior. Attempting to unlock the mutex if it is not locked results in undefined behavior.

    [...]

    Errors
    [...]
    The pthread_mutex_unlock() function may fail if:

    EPERM
    The current thread does not own the mutex.
    Ergo: se il mutex è di tipo PTHREAD_MUTEX_NORMAL o PTHREAD_MUTEX_DEFAULT ottieni undefined behavior (ovvero, può succedere qualunque cosa), se è di tipo PTHREAD_MUTEX_ERRORCHECK o PTHREAD_MUTEX_RECURSIVE ottieni l'errore EPERM.
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it L'avatar di MrX87
    Registrato dal
    Jun 2007
    Messaggi
    500
    ok......bhè ma io intendevo fare una cosa del tipo:

    codice:
    BUFFER B;
    
    A {
      lock(mutex1)
      sleep 10
    }
    
    B {
      unlock(mutex1)
      .....
      
    }
    in questo modo supponendo che A e B sono eseguiti da 2 threads differenti ho una lock di mutex1 in un thread e una unlock di mutex1 in un altro thread...poi stai a vedere l'utilità....a questo punto nn saprei.....
    "Non può piovere per sempre" Il Corvo
    Forza Vigor!

  4. #4
    Come detto sopra, in generale non è un'operazione supportata - il mutex deve essere sbloccato dallo stesso thread che l'ha bloccato, altrimenti ottieni un errore o (peggio) undefined behavior.
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.