1 / 11

Lab3

Lab3. TA: Yi Cui 02/12/2013. Part 3. Goals Practice multiple threads programming Difficulties Synchronization Exit condition. Part 3. What does the big picture of part 3 look like? Your threads CC process Robots. Synchronization. Use critical section to protect shared data

Download Presentation

Lab3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab3 TA: Yi Cui 02/12/2013

  2. Part 3 • Goals • Practice multiple threads programming • Difficulties • Synchronization • Exit condition

  3. Part 3 • What does the big picture of part 3 look like? • Your threads • CC process • Robots

  4. Synchronization • Use critical section to protect shared data • Notification of exit • Program termination

  5. Critical section • Where do we need to use critical section? Search() { U.add (s, 0); D.add (s); while ( U.notEmpty () ) t = U.removeNextTuple () if ( t.ID == T ) break N = G.getNeighbors (t) for each y in N if ( D.find (y) == false ) U.add (y) D.add (y) }

  6. Exit condition • Find exit • No more nodes to be searched • U is empty • No active threads • What is an active thread?

  7. Notification of exit • Set a boolean flag: exit = true, false ? • Set event ?

  8. Program termination HANDLE *hSearch = new HANDLE[robot]; for (int i = 0; i < robot; i++) hSearch[i] = CreateThread(...); // wait for all threads to finish for (int i = 0; i < robot; i++) WaitForSingleObject(hSearch[i], INFINITE); DisconnectRobots(...); DisconnectCC(...);

  9. Passing parameters (C style) struct Parameters { HANDLE mutex; U D int activeThread; int totalExtracted; }; DWORD WINAPI SearchThread(LPVOID lpPara) { Parameters *p = (Parameters*)lpPara; p->U ... } Parameters para; handles[i] = CreateThread(..., SearchThread, &para, ....);

  10. Passing parameters (C++ style) class GraphSearch { public: void search(); private: HANDLE mutex; int totalExtracted; ... }; DWORD WINAPI SearchThread(LPVOID lpPara) { GraphSearch *p = (GraphSearch*)lpPara; p->search(); ... } GraphSearch gs; handles[i] = CreateThread(..., SearchThread, &gs, ....);

  11. Misc • Suppose we have 5,000 threads • Where do we create 5,000 pipes? • Where do we do 5,000 initial connection? • Initial room is always the same • Shared data • Pass by reference (do not make multiple copy of it)

More Related