void* Stealer(void* param){
	WorkStealQueue<ObjType*> *q = (WorkStealQueue<ObjType*> *)param;

	ObjType* r; 
	for(int i=0; i<nStealAttempts; i++){
		if (q->Steal(r)) 
			r->Operation(); 
	}
	return 0;
}
int main(int argc, char** argv) {
  args(argc, argv);
  pthread_t* handles = new pthread_t[nStealers];
	ObjType* items = new ObjType[nItems];

	WorkStealQueue<ObjType*> *q = new WorkStealQueue<ObjType*>(INITQSIZE);

	for (int i = 0; i < nStealers; i++) {
		pthread_create(&handles[i], NULL, Stealer, (void*)q);
	}

	
	for (int i = 0; i < nItems/2; i++) {
		q->Push(&items[2*i]);
		q->Push(&items[2*i+1]);

		ObjType* r; 
		if (q->Pop(r)) 
			r->Operation(); 
	}

	for (int i = 0; i < nItems/2; i++) {
		ObjType* r; 
		if (q->Pop(r)) 
			r->Operation(); 
	}


	for (int i = 0; i < nStealers; i++) {
    pthread_join(handles[i], NULL);
  }

	for (int i = 0; i < nItems; i++) {
		items[i].Check();
	}

	delete[] items;
	delete[] handles;
	delete q;
	return 0;
}
Beispiel #3
0
__declspec(dllexport) int ChessTestRun(){
	HANDLE*  handles = new HANDLE[nStealers];
	ObjType* items = new ObjType[nItems];

	WorkStealQueue<ObjType*> *q = new WorkStealQueue<ObjType*>(INITQSIZE);

	for (int i = 0; i < nStealers; i++) {
		DWORD tid;
		handles[i] = CreateThread(NULL, 0, Stealer, q, 0, &tid);
	}

	
	for (int i = 0; i < nItems/2; i++) {
		q->Push(&items[2*i]);
		q->Push(&items[2*i+1]);

		ObjType* r; 
		if (q->Pop(r)) 
			r->Operation(); 
	}

	for (int i = 0; i < nItems/2; i++) {
		ObjType* r; 
		if (q->Pop(r)) 
			r->Operation(); 
	}


	WaitForMultipleObjects(nStealers, handles, TRUE, INFINITE);

	for (int i = 0; i < nItems; i++) {
		//items[i].Check();
	}

	delete[] items;
	delete[] handles;
	delete q;
	return 0;
}