2

I am trying to port some C files from windows to linux. I have ran into Windows mutex functions, such as: CreteMutex(), OpenMutex(), CloseHandle(), etc... The author of these files creates the mutexes with names and checks if they exist by their name. I looked into pthreads a little and found that you cannot attach names to them. Are pthreads my best bet or are there other mutexes I can use in linux?

SudoObey
  • 99
  • 8
  • 1
    You can put a `pthread_mutex_t` in a file and share it between processes by `mmap`ing (see this recent question for some code https://stackoverflow.com/q/52848184/1076479). But you can also use `sem_open(3)` which already uses a filesystem name to establish a rendezvous point. More info at http://man7.org/linux/man-pages/man7/sem_overview.7.html – Gil Hamilton Oct 17 '18 at 20:18
  • Named mutexes in windows are ones shared between different processes, IIRC. pthreads lets you do the same thing, but the process is quite different. – Shawn Oct 17 '18 at 20:19
  • If it has to be named, then I believe you have to use a semaphore on Linux. Also see [`sem_open`](http://pubs.opengroup.org/onlinepubs/009604499/functions/sem_open.html) and friends, [Share named POSIX semaphores](https://stackoverflow.com/q/50702902/608639), [How to use named mutex at linux?](https://stackoverflow.com/q/46313748/608639), etc. – jww Oct 18 '18 at 02:52

0 Answers0