Commit 880ff9d3 authored by Emeric Verschuur's avatar Emeric Verschuur

Amélioration de la gestion des threads

parent 3e670ae3
......@@ -23,7 +23,6 @@
#include <fstream>
#include <iostream>
#include <cassert>
#include <vector>
#ifdef WIN32
#define msleep(s) Sleep(s)
#else
......@@ -135,13 +134,24 @@ void LrScene::setCamera(LrCamera *camera)
struct __lr_generateimage_t{
LrScene *scene;
unsigned int lOffset;
unsigned int lNb;
int it;
pthread_mutex_t mut;
};
static void *__lr_generateimage(void *ptr){
__lr_generateimage_t *p=(__lr_generateimage_t*)ptr;
p->scene->generateImagePart(p->lOffset,p->lNb);
int it;
while(true){
pthread_mutex_lock(&(p->mut));
p->it++;
if(p->it >= p->scene->getImage()->getHeight()){
pthread_mutex_unlock(&(p->mut));
return NULL;
}
it=p->it;
pthread_mutex_unlock(&(p->mut));
p->scene->generateImagePart(it,1);
}
return NULL;
}
......@@ -152,21 +162,16 @@ void LrScene::generateImage(int nbThreads)
cout << "[.........10........20........30........40........50........60........70........80........90.........]\r[ " << flush;
int nbL = m_image->getHeight();
int nbLQ = nbL / nbThreads;
assert(nbThreads > 0);
assert(nbThreads < nbL);
assert(nbL % nbThreads == 0);
vector<pthread_t> thLst;
vector<__lr_generateimage_t> ptrLst;
thLst.resize(nbThreads);
ptrLst.resize(nbThreads);
__lr_generateimage_t ptr={this,-1,PTHREAD_MUTEX_INITIALIZER};
pthread_t thLst[nbThreads];
for( int i=0 ; i < nbThreads ; i++ ){
ptrLst[i].scene = this;
ptrLst[i].lOffset = nbLQ * i;
ptrLst[i].lNb = nbLQ;
pthread_create(&thLst[i],NULL,__lr_generateimage,(void*)&ptrLst[i]);
pthread_create(&thLst[i],NULL,__lr_generateimage,(void*)&ptr);
}
const char *curs="-\\|/";
......@@ -182,8 +187,8 @@ void LrScene::generateImage(int nbThreads)
cout << (char)8 << '#' << ' ' << flush;
}
for( vector<pthread_t>::iterator it=thLst.begin();it!=thLst.end();it++){
pthread_join(*it,NULL);
for( int i=0 ; i < nbThreads ; i++ ){
pthread_join(thLst[i],NULL);
}
cout << (char)8 << ']' << endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment