In order to share a mutex using pthreads, you are going to have to somehow share the address of the mutex through a pointer or make the mutex a globally accessible variable.
In order to share a mutex using pthreads, you are going to have to somehow share the address of the mutex through a pointer or make the mutex a globally accessible variable. Mutexes or semaphores themselves do not lock a given memory or critical code section ... instead they lock access to a specific "flag" or location in memory (i.e. , like an unsigned long or some other POD type) that is then used to determine access to a critical section or other globally accessible memory location.In other words once a thread "owns" a given mutex, it gets to have access to a segment of code or memory section that any other thread trying to obtain ownership of that same mutex is blocked from for the duration of the owning thread's lock.
If you use two different mutexes to block access to a given location, that does not provide mutually exclusive access to the actual memory segment ... each thread that does not share a mutex will have equal access to the memory segment even though they may each have an ownership lock on their respective mutexes. Therefore the memory segment is not really protected ... two threads could access the memory segment at the same time, creating concurrency issues.So again, if you have different modules or threads, and you want to have exclusive access to a given section of code or memory, you're going to have to share the same mutex between those elements. There are many ways this could be done, either though something like named semaphores if you need to-do this for multiple separate processes, or through a shared memory segment (i.e.
, shmget(), shmat(), shmdt, etc.), etc. If you can't somehow share a globally accessible mutex or pointer because of some separation of address space between your modules.
– Helium3 May 10 at 22:31 Yes, that could be one way of accessing it ... declare the mutex as extern inside a header file, and then it the associated . C or . Cpp file initialize the mutex, using something like pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;.
That way you can define the mutex without having to use a dedicated initialization function, so any other module accessing the mutex can be assured that it's already properly initialized and not have to call an initialization function. You could also declare the mutext as static inside a . C or .
Cpp file, and use a function to return a pointer to it. – Jason May 10 at 22:39 The "other module" that needs the mutex will define it as extern right? Yeah I have it declared as pthread_mutex_t someMutex = PTHREAD_MUTEX_INITIALIZER; – Helium3 May 10 at 22:43 I have them as static at the moment.
Should I change it to extern for it(the same mutex) to be used in another file – Helium3 May 10 at 22:44 1 Okay, just to clear things up before they get confusing. If you want the mutex externally available, there are two options: 1) declare them static inside you . C file and then create an externally available function that returns a pointer to the mutex, or 2) Declare the mutex as extern in a header file once, and then define/initialize the mutex once as a non-static variable inside an associated .
C file (no need for the extern keyword in the . C file). Then any other module that needs access to the mutex just has to include the appropriate header file.
– Jason May 10 at 23:00.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.