Click here to load reader

Lab3

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Slide 1

TA: Yi Cui02/12/2013Lab3Part 3GoalsPractice multiple threads programmingDifficultiesSynchronizationExit conditionPart 3What does the big picture of part 3 look like?Your threadsCC processRobotsSynchronizationUse critical section to protect shared dataNotification of exitProgram terminationCritical sectionWhere 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 ) breakN = G.getNeighbors (t) for each y in N if ( D.find (y) == false ) U.add (y) D.add (y)}Exit conditionFind exitNo more nodes to be searchedU is emptyNo active threadsWhat is an active thread?Notification of exitSet a boolean flag: exit = true, false ?Set event ?Program terminationHANDLE *hSearch = new HANDLE[robot];for (int i = 0; i < robot; i++)hSearch[i] = CreateThread(...);

// wait for all threads to finishfor (int i = 0; i < robot; i++)WaitForSingleObject(hSearch[i], INFINITE);

DisconnectRobots(...);

DisconnectCC(...);Passing parameters (C style)struct Parameters{HANDLE mutex;UDint activeThread;int totalExtracted;};

DWORD WINAPI SearchThread(LPVOID lpPara){Parameters *p = (Parameters*)lpPara;p->U...}

Parameters para;handles[i] = CreateThread(..., SearchThread, &para, ....);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, ....);

MiscSuppose we have 5,000 threadsWhere do we create 5,000 pipes?Where do we do 5,000 initial connection? Initial room is always the sameShared dataPass by reference (do not make multiple copy of it)