If I have a module that has a mutex and I write the value of an int variable using lock/unlock on the mutex, how is the same mutex locked/unlocked in another module that is being run in a thread?
The external module also needs to write the value of the variable and this is being done in a threaded loop function. How does one lock this using the same mutex or should another mutex be used?
What happens if 2 different mutexes lock the same memory segments(not necessarily simultaneously)?
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.
Related
Is it possible to use mutex from pthread.h to synchronize processes created with fork() from unistd.h? Afaik, both in the end are using system call clone().
I am asking it in the scope of shared memory segment (from ipc.h, shm.h) with critical data, which should be protected against concurrent writes from different processes. In that memory then semaphores can be defined and later used in different processes. Why couldn't mutexes be used instead of semaphores?
Why am I asking?
First of all I was told that it won't work, without receiving any explanation for that. On the Internet I was not able to find any answer so I decided to ask here.
Second, forked process is safer than thread created with pthread_create - if forked process crashes, the rest of the program continues to work and if thread crashes then whole program exits.
Third, mutexes seem to be more human-friendly than semaphores in managing.
I am writing an application that requires that certain activities for a given user are not stomped on by possibly competing threads. My entire user database is in memory and I was thinking of adding a pthread_rwlock_t to the user data structure. I do not anticipate more than about 10 to 20 thousand users. At 56 bytes for the lock structure that's not a lot of RAM at all. My question is, is there a practical limitation to the number of actual rwlocks you can have in a process? Please note I am NOT talking about the number of threads that can get a lock, or the number of times a given thread can increment the lock counter. Rather, I am wondering if there is some underlying kernel or other resource that backs each individual lock that I may end up exhausting.
This is a quality-of-implementation issue: POSIX allows the initialisation of a rwlock to fail due to resource exhaustion. However, common Linux implementations, for example, don't require any per-lock resources other than the memory for the pthread_rwlock_t itself.
In my textbook it says:
All threads within a process have access to the same data (share)
But each thread has its own stack, it means local variable is not shared. So what kind of data threads can share.
update:
I found each thread can share global variable, it made me confused, what I learn global global variable is static stack, it shouldn't be shared by that each thread has its own stack.
Firstly, global and static variables are shared.
Secondly, memory from malloc that you can reach via a pointer in a global variable, (or via a pointer that...) is shared.
Thirdly, memory in one thread's stack that you can reach via a pointer in a global variable, (or via a pointer that...) is shared.
Really, all of it is shared, all the time, but not all of it is reachable. Thread A can access thread B's stack, but it won't have a pointer to do so through unless thread B does something like assign the address of something in its stack to a global (don't do that) or you're doing something where you examine the details of the threads and work your way into their stacks (if you're doing that you're way more knowledgeable enough about the mechanisms of the pthreads implementation than I am*, so I won't tell you not to do that, but as a rule it wouldn't be something to do lightly).
Mostly you only have to worry about global and static, and can consider anything dealing only in locals to be thread-safe, but that gets blown away once you can reach the latter from the former.
*Actually, I know little on that, and am mostly basing this on knowledge of threading in other languages. Really, it's a general threading thing rather than a pthreads specific one, with the exception that some other languages won't even let you fall into the trap of referencing stack memory from global.
I found each thread can share global variable, it made me confused,
what I learn global global variable is static stack, it shouldn't be
shared by that each thread has its own stack.
You're thinking too much about the stack, there are other memory regions such as data or bss. Objects with static storage (such as global variables and those declared with the modifier static) are shared between all threads.
Also, if you try hard, all threads can acces everything, there's nothing special about a different "stack". If a thread manages to get a pointer to a location on another "stack" it can freely read it, modify it etc.
The main point of all this is that threads don't just share variables. They are simply in the same virtual memory space.
#cnicutar is right about the static storage. Actually there is a good discussion about this in another question. Despite the title of that question, the answers there(especially first two) do answer your question well and I don't think I can do better.
As has been said, each thread has its own stack. So data on this stack is said to be thread safe, since only it owns it.
All threads within a process have access to the same data (share)
As it implies, multiple threads have access to same data. This should ring some mini alarm bells since you have to start thinking about thread synchronization (critical sections, mutexes etc) to resources which are shared.
Resources allocated on the heap say through the new operator are shared as all threads have access to the same heap.
I dont think static data will be allocated for each thread.
It will be instantiated only once and will be accessible to all the threads having the declaration of that static data in their execute() method..
pthread_mutex_trylock detects deadlocks, doesn't block, then why would you even "need" pthread_mutex_lock?
Perhaps when you deliberately want the thread to block? But in that case it may result in a deadlock?
pthread_mutex_trylock does not detect deadlocks.
You can use it to avoid deadlocks but you have to do that by wrapping your own code around it, effectively multiple calls to pthread_mutex_trylock in a loop with a time-out, after which your thread releases all its resources.
In any case, you can avoid deadlocks even with pthread_mutex_lock if you just follow the simple rule that all threads allocate resources in the same order.
You use pthread_mutex_lock if you just want to efficiently wait until the resource is available, without having to spin on the mutex, something which is often very inefficient. Properly designed multi-threaded applications have no need for the pthread_mutex_trylock variant.
Locks should only be held for the absolute minimum time to do the work and, if that's too long, you can generally redesign things so the lock time is less (such as by using the mutex to only copy data to a thread's local data areas, and having the long-running bit work on that after the mutex is released).
The pseudo-code:
while not pthread_mutex_trylock:
yield
will continue to run your thread, waiting for the lock to be available, especially since there is no pthread_yield() in POSIX threads (though it's sometimes provided as a non-portable extension).
That means, at worst, the code segment above won't even be able to portably yield the CPU, therefore chewing up the rest of it's quantum every time through the scheduler cycle.
And at best, it will still activate the thread once per scheduler cycle just to see if the mutex can be obtained.
Whereas:
pthread_mutex_lock
will most likely totally pause your thread until the lock is made available, since it will move it to a waiting queue until the current lock holder releases the mutex.
That's probably the major reason why you should prefer pthread_mutex_lock to pthread_mutex_trylock.
Perhaps when you deliberately want the thread to block?
Yup, exactly in this case. But you can mimic pthread_mutex_lock() behavior with something like that
while(pthread_mutex_trylock(&mtx))
pthread_yield()
I understand the basic concept of stack and heap but great if any1 can solve following confusions:
Is there a single stack for entire application process or for each thread starting in a project a new stack is created?
Is there a single Heap for entire application process or for each thread starting in a project a new stack is created?
If Stack are created for each thread, then how process manage sequential flow of threads (and hence stacks)
There is a separate stack for every thread. This is true not only for CLR, and not only for Windows, but pretty much for every OS or platform out there.
There is single heap for every Application Domain. A single process may run several app domains at once. A single app domain may run several threads.
To be more precise, there are usually two heaps per domain: one regular and one for really large objects (like, say, a 64K array).
I don't understand what you mean by "sequential flow of threads".
One stack for each thread, all threads share the same heaps.
There is no 'sequential flow' of threads. A thread is an operating system object that stores a copy of the processor state. The processor state includes the register values. One of them is ESP, the stack pointer. Another really important one is EIP, the instruction pointer. When the operating system switches between threads, it stores the processor state in the current thread object and reloads the state from the thread object for the thread that was selected to run next. The processor now simply continues executing where it left off previously.
Getting a thread started is perhaps now easy to understand as well. The operating system allocates a megabyte of memory for the stack. And initializes the ESP register value to point to that memory. And sets the value of the EIP register to the address of the method where the thread should start executing. The value of the ThreadStart delegate in C#.
Each thread must have it's own stack, that's where local variables and parameters are held, and the return addresses of the previous functions.