Пример #1
0
int main() {
  char now[101];
  strcpy(now, "http://www.acm.org/");
  for(;;) {
    char s[71];
    scanf("%s", s);
    if (strcmp(s, "VISIT") == 0) {
      scanf("%s", s);
      printf("%s\n", s);
      b_push(now);
      strcpy(now, s);
      empty();
    } else if (strcmp(s, "BACK") == 0) {
      char *url=b_pop();
      if (strcmp(url, "Ignored")!=0) {
        f_push(now);
        strcpy(now, url);
      }
      printf("%s\n", url);
    } else if (strcmp(s, "FORWARD") == 0) {
      char *url=f_pop();
      if (strcmp(url, "Ignored")!=0) {
        b_push(now);
        strcpy(now, url);
      }
      printf("%s\n", url);
    } else {
      return 0;
    }
  }
}
int main(void) {
    struct stack *balancer = stack_new();
    for (int i = 0; i < 10; i++) {
        b_push(balancer, node_new((void *)0xbeef), 2);
        printf("after push, n stacks in the balancer: %d\n",balancer->n_items);
    }

    for (int i = 0; i < 10; i++) {
        struct node *n = b_pop(balancer);
        printf("after pop, n stacks in the balancer: %d, val: %p\n",balancer->n_items, n->value);
    }

}
Пример #3
0
void BucketSort::sort(unsigned int numCores) {

	std::vector<std::shared_ptr<std::thread>> threads1;

	std::function<void(unsigned int id)> b_push;
	b_push = [this, &numCores] (unsigned int id) {
		int num;
		for (unsigned int i=0;i<numbersToSort.size();++i) {
			//std::cout << i << " " << id << " " << numCores << " " << numbersToSort.size()-numCores << std::endl;
			num = BucketSort::lead(numbersToSort[i]);
			if (num%numCores == id)	vecs[num].push_back(numbersToSort[i]);
		}
	};

	for (unsigned int i = 1; i < numCores; ++i) {
		threads1.push_back(std::make_shared<std::thread>(b_push, i));
	}

	b_push(0);
	
	for (auto t : threads1) {
		t->join();
	}

	/*
	for (unsigned int n : numbersToSort) {
		vecs[BucketSort::lead(n)].push_back(n);
	}
	*/

	int work = 9;
	std::mutex mutexWork;

	std::function<void()> b_sort;
	b_sort = [this, &work, &mutexWork, &b_sort] () {
			
			mutexWork.lock();
			//std::cout << "working on: " << work << std::endl;
			int i = work--;
			mutexWork.unlock();
			
			std::sort(vecs[i].begin(),vecs[i].end());
			if (work>0) b_sort();
	};
	
	std::vector<std::shared_ptr<std::thread>> threads2;

	for (unsigned int i = 1; i < numCores; ++i) {
		threads2.push_back(std::make_shared<std::thread>(b_sort));
	}

	b_sort();
	
	numbersToSort.clear();
	numbersToSort.reserve(vecs[1].size() + vecs[2].size() + vecs[3].size() + vecs[4].size() + vecs[5].size()
												+ vecs[6].size() + vecs[7].size() + vecs[8].size() + vecs[9].size());

	for (auto t : threads2) {
		t->join();
	}
	
	numbersToSort = std::move(vecs[1]);
    std::move(std::begin(vecs[2]), std::end(vecs[2]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[3]), std::end(vecs[3]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[4]), std::end(vecs[4]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[5]), std::end(vecs[5]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[6]), std::end(vecs[6]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[7]), std::end(vecs[7]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[8]), std::end(vecs[8]), std::back_inserter(numbersToSort));
    std::move(std::begin(vecs[9]), std::end(vecs[9]), std::back_inserter(numbersToSort));

}