🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Jak najlepiej dzielić dane wspólne dla wątków?

#309635
From: Borneq
Date: Mon, 29 Jan 2018 12:22
46 lines
2369 bytes
Mam gist:
https://gist.github.com/borneq/f63cf132abef474dc9e25491d3115351
void FinderForBlock::execute(vector<InputTx> *inputVec, vector<OutputTx> 
*outputVec)
{
     FinderForTx finder;
     for (size_t i = 0; i<inputVec->size(); i++)
     {
         outputVec->at(i) = finder.findPred(inputVec->at(i));
     }
}

vector<thread> vec_thr;
for (int i=0; i<nThreads; i++)
     vec_thr.emplace_back(&execute, &inputVec, &outputVec);
for (auto& t : vec_thr)
     t.join()

(pytałem się na 
https://social.msdn.microsoft.com/Forums/en-US/37c1cf02-6a54-47d3-ba1c-a631239f81e8/how-to-share-thread-data?forum=vcgeneral 
ale nie chcę TLS)

Chodzi o to, że execute dostaje jako parametry dwa wektory a ma jeszcze 
dostać indeks wspólny i muteks, który ma nie być globalny.
Kiedyś robiłem to tak, że definiowałem strukturę z muteksem, indeksem i 
innymi rzeczami i dawałem wskaźnik na nią do wszystkich wątków:
void FinderForBlock::execute(Structure *structure)
{
     FinderForTx finder;
     while(true)
     {
       structure->mutex.lock();
        if (structure->index>=structure->vecSizes)
       {
         structure->mutex.unlock();
         break;
       }
       structure->index++;
       structure->mutex.unlock();
       outputVec->at(structure->index-1) = 
finder.findPred(inputVec->at(structure->index-1));
     }
}

Czy sposób ze strukturą jest dobry? A zamiast niej chciałbym użyć klasy 
FindeForBlock - jak?
Technical Details
Message-ID: <p4n3n1$fom$1@node1.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

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: Jak najlepiej dzielić dane wspólne
 dla wątków?
Date: Mon, 29 Jan 2018 13:22:59 +0100
Organization: ATMAN - ATM S.A.
Lines: 46
Message-ID: <p4n3n1$fom$1@node1.news.atman.pl>
NNTP-Posting-Host: public-gprs350495.centertel.pl
Mime-Version: 1.0
X-Trace: node1.news.atman.pl 1517228577 16150 37.47.0.96 (29 Jan 2018 12:22:57 GMT)
X-Complaints-To: usenet@atman.pl
NNTP-Posting-Date: Mon, 29 Jan 2018 12:22:57 +0000 (UTC)
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2
X-Mozilla-News-Host: news://news.atman.pl:119
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit