简体   繁体   中英

Multithreading in multi cpu multithreading

Suppose I have an undirected graph.

A small portion of the graph :

A -----\
        C
B -----/

Now the node A and B proceeds to modify parallely node C. // Node A and Node B process Node C in parallel thread.

I want the thread for Node B will wait for thread for Node A to complete processing. It has to be this way. No other way possible.

I also don't want other thread that are processing other Nodes to wait for the above condition, ie, I don't want to use mutex.

I have created a class member boolean flag and want to use that as a condition. // let that flag be d_flag

MT here uses multiple CPUs.

Solution that I think doesn't works :

CPU1 : Thread 1

while (d_flag) {
    // busy waiting
}
d_flag = true;

// Critical Section

d_flag = false;

CPU2 : Thread 1

while (d_flag) {
   // busy waiting
}

d_flag = true;

// Critical Section

d_flag = false;

My understanding is that if in multiple CPU, Node A thread is running on CPU1 and Node B thread on CPU 2, the spin lock is defeated.

There may be a condition where both the while loop evaluates to false as the test in the while loop is no longer atomic.

Is my understanding correct????? If yes, is there is a solution without using overall mutex.

I would suggest creating a conditional branch which executes only when the node being processed is C. In that branch you could setup a condition variable for the node B thread to wait until the node A thread has finished processing node C. Assuming you know the thread IDs associated to the nodes, I would imagine this to be fairly easy to implement (at least naively.) You would probably want to implement a conditional variable with a timeout if you aren't certain node A's thread would ever complete processing node C.

If I may make a suggestion, your use case having constraints on the ordering of operations on nodes for specific threads suggests you might want to consider a parallel programming paradigm which allows you to express these ideas at a higher level of abstraction. Perhaps using the actor pattern would help?

Here's an implementation which I think it pretty good for C++:

https://github.com/Neverlord/libcppa

http://libcppa.blogspot.com/

Using a pattern like that, you could have your actors (threads) communicate about which nodes they have processed and control their flow based on those communications. So for example, you could have the node A actor inform the node B actor when it has finished processing node C. This allows the node B actor to do other things while waiting for the message from A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM