Domanda

There are 3 tools. A consumer needs 2 tools to modify the buffer. If consumer A takes the 2 tools and consumer B takes 1, consumer B will have to wait for another tool to be released.

I am not sure if I'm thinking about this problem in the right way. The way I interpret it is that I need 3 mutex and the consumer has to lock 2 out 3, is this the right idea?

I don't think I should be using semaphores because I don't want multiple threads accessing a shared resource at the same time. In a normal producer consumer problem there is just 1 mutex lock but here I need any 2 of the 3 how do I approach this?

È stato utile?

Soluzione

Yes, you can use 3 mutexes, but you must be careful to avoid deadlock. To do so, you must establish a well known order to acquire locks: for example, always acquire the lock for the tool with the lowest identifier first. In situations like these, avoiding deadlock is always a matter of acquiring locks in the same order.

Some pseudo-code:

pthread_mutex_t locks[3]; // 1 lock for each tool

critical_function() {
    /* Acquire 2 tools, t1 and t2 */
    first = min(t1, t2);
    second = max(t1, t2);
    locks[first].lock();
    locks[second].lock();
    /* Do work... */
    locks[first].unlock();
    locks[second].unlock();
}

This assumes that you associate an ID to each tool in the range 0-2.

But notice, if you will, that no more than one producer / consumer can be working at the same time, since there are 3 tools only. So you might want to use a single mutex instead, lock it, acquire 2 tools, do the work and release it - after all, there is not much possibility for parallelism, you will have at most one producer / consumer working, and the others waiting while holding 1 tool.

Altri suggerimenti

Well, first of all see what are your shared resources? Tools of course. Now there are three tools and two of them are required to do work. So we need to be careful whenever a consumer wants to acquire tools. Suppose the function AcquireTools() assigns the tools to a consumer. Now this can easily be solved. First we declare a global variable mutex and initialize it to 1. Then,

wait(mutex);   
AcquireTools();
//do work
signal(mutex);

suppose consumerA wants the two tools, it will call wait(mutex) and the value of mutex changes to 0. Now while consumerA is working, consumerB wants the two tools. It will call wait(mutex), but the value of mutex=0, so it can't go ahead until consumerA signals and the value of mutex changes back to 1.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top