🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.c
Article #309618

Re: Szukam przykładu thread_pool

#309618
From: Borneq
Date: Sun, 14 Jan 2018 19:44
51 lines
1913 bytes
Biorę implementację z https://github.com/progschj/ThreadPool
wpisuje do puli za pomocą emplace_back
potem odpytuję:
	for (auto &&result : results)
		std::cout << result.get() << std::endl;
ALE to ma wadę: jeśli pierwszy result trwa najdłużej, to czeka na niego, 
potem szybko wypluwa wyniki dalsze:521. A ja bym chciał od razu dostawać 
125.

#include <iostream>
#include "ThreadPool.h" //https://github.com/progschj/ThreadPool

int main()
{
	ThreadPool pool(8);

	std::vector<std::future<int>> results;

	results.emplace_back(
		pool.enqueue([]
	{
		std::this_thread::sleep_for(std::chrono::seconds(5));
		return 5;
	})
	);


	results.emplace_back(
		pool.enqueue([]
	{
		std::this_thread::sleep_for(std::chrono::seconds(2));
		return 2;
	})
	);


	results.emplace_back(
		pool.enqueue([]
	{
		std::this_thread::sleep_for(std::chrono::seconds(1));
		return 1;
	})
	);
	
	for (auto &&result : results)
		std::cout << result.get() << std::endl;

	while(true);
	return 0;

}
Technical Details
Message-ID: <p3gbu6$nqa$1@node2.news.atman.pl>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail
References: <p3d0dt$k1h$1@node2.news.atman.pl>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail
From: Borneq <borneq@antyspam.hidden.pl>
Newsgroups: pl.comp.lang.c
Subject: Re: Szukam przykładu thread_pool
Date: Sun, 14 Jan 2018 20:44:06 +0100
Organization: ATMAN - ATM S.A.
Lines: 51
Message-ID: <p3gbu6$nqa$1@node2.news.atman.pl>
References: <p3d0dt$k1h$1@node2.news.atman.pl>
NNTP-Posting-Host: public-gprs352921.centertel.pl
Mime-Version: 1.0
X-Trace: node2.news.atman.pl 1515959046 24394 37.47.9.218 (14 Jan 2018 19:44:06 GMT)
X-Complaints-To: usenet@atman.pl
NNTP-Posting-Date: Sun, 14 Jan 2018 19:44:06 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2
In-Reply-To: <p3d0dt$k1h$1@node2.news.atman.pl>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit