Bonjour,
J'ai dernièrement poster des messages concernant l'utilisation des pthreads en c++.
Je voudrais utiliser les threads dans le cadre du calcul multithread de la dct.

Voici mes différents fichiers.

Ma classe DCT:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
#ifndef DCT_H
#define DCT_H
 
 
#include <opencv/highgui.h>
#include <GL/gl.h>
#include <opencv/cxcore.h>
#include <opencv/cv.h>
using namespace cv;
 
 
#include <pthreaddct.h>
 
class DCT
{
    Mat ImOr;
 
public:
    DCT(Mat &Im){ImOr = Im.clone(); imshow("DCT", ImOr);cvWaitKey(1000);}
    void Process();
};
 
#include "dct.h"
 
#include <iostream>
 
 
void DCT::Process()
{
    cout << "DCT::Process() -1" << endl;
    PthreadDCT *tr;
 
    cout << "DCT::Process() -0.5\n" << endl;
    BufferDCT *DCTBuf = new BufferDCT(640,480,8);
 
    Mat *R,*G,*B;
    ExtremumValue *Ex = new ExtremumValue();
 
    cout << "DCT::Process() 0" << endl;
    tr = new PthreadDCT(DCTBuf,R,G,B,&ImOr,Ex);
 
    cout << "DCT::Process() 1" << endl;
    tr->Exec(NULL);
}
 
 
#endif // DCT_H
Ma classe pthreaddct:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
 
#ifndef PTHREADDCT_H
#define PTHREADDCT_H
 
#include <opencv/highgui.h>
#include <GL/gl.h>
#include <opencv/cxcore.h>
#include <opencv/cv.h>
using namespace cv;
 
#include <pthreadcpp.h>
 
class BufferDCT;
class ExtremumValue;
class Block;
class PthreadDCT: protected PThread
{
 
    Mat *R,*G,*B;
    Mat * ImOr;
    BufferDCT *DCTBuf;
    int nChannels;
    int Quantificator;
 
    ExtremumValue *Ex;
 
 
public:
    PthreadDCT(BufferDCT * pDCTBuf, Mat *Rin, Mat *Gin, Mat *Bin, Mat *ImOrin,ExtremumValue * Exin);
    void * Process(void *);
    void Exec(void *);
 
    friend void * ThrdProcess(void *targ) ; // la fonction appelée par pthread_create
    void ProcessBlock(Block *B , int offset);
 
};
 
 
class ExtremumValue
{
public:
    double Min;
    double Max;
    pthread_mutex_t mutex;
    ExtremumValue(){mutex = PTHREAD_MUTEX_INITIALIZER;}
};
 
class Block
{
 
public:
    int X;
    int Y;
    int Processed;
    double tab[64*3];
 
    Block(int Xin,int Yin){X=Xin;Y=Yin;Processed=0;}
};
 
 
#include <vector>
#include <list>
 
using namespace std;
 
 
class BufferDCT
{
 
    int W,H;
public:
    vector <Block *> * Regions;
    pthread_mutex_t mutex;
 
    BufferDCT(int width,int height,int Block_size);
};
#endif // PTHREADDCT_H
 
#include "pthreaddct.h"
 
#include <iostream>
 
 
////////////////////////////////////////////////////
/// \brief ThrdProcess
/// \param arg
/// \return
///
void * ThrdProcess(void *arg)
{
    PthreadDCT *Thrd = (PthreadDCT *)arg;
    if(Thrd != NULL &&  !Thrd->IsRunning() )
    {
        Thrd->running   = true;
        Thrd->finished  = false;
        cout << "ThrdProcess(void *arg)0" << endl;
        cout << "Thrd->nChannels " << Thrd->nChannels << endl;
        imshow("ThrdProcess",*(Thrd->ImOr));
        cout << "ThrdProcess(void *arg)1" << endl;
        while(1)
        {
            cvWaitKey(10);
        }
        Thrd->Process(arg) ;
 
        Thrd->running   = false;
        Thrd->finished  = true;
    }
 
}
 
 
/////////////CLASS PthreadDCT
 
/////////////////////////////////////////////////////
/// \brief PthreadDCT::PthreadDCT
/// \param pDCTBuf
///
PthreadDCT::PthreadDCT(BufferDCT * pDCTBuf, Mat *Rin, Mat *Gin, Mat *Bin, Mat *ImOrin, ExtremumValue *Exin):
    PThread()
{
 
    DCTBuf      = pDCTBuf;
    R           = Rin;
    G           = Gin;
    B           = Bin;
    ImOr        = ImOrin;
    nChannels   = 3;
 
    cout << "PthreadDCT::PthreadDCT0" << endl;
    imshow("PthreadDCT",*ImOrin);
    cvWaitKey(10000);
    cout << "PthreadDCT::PthreadDCT1" << endl;
    //while(1){cvWaitKey(10);}
    Quantificator = 25;
 
    Ex          = Exin;
 
}
 
////////////////////////////////////////////////////
/// \brief PthreadDCT::Process
/// \param targ
/// \return
///
void* PthreadDCT::Process(void *targ)
{
    int i = 0;
    PthreadDCT *t = (PthreadDCT *) targ;
 
    cout << "PthreadDCT::Process" << endl;
    imshow("PthreadDCT::Process",*t->ImOr);
    cvWaitKey(10000);
 
    while(1)
    {
 
        pthread_mutex_lock(&t->DCTBuf->mutex);
 
            while((*t->DCTBuf->Regions)[i]->Processed !=0 && t->DCTBuf->Regions->size() ){i++;}
 
            if(i < t->DCTBuf->Regions->size() )
            {
                (*t->DCTBuf->Regions)[i]->Processed++; // Fermeture de l'acces au block
 
                cout << "i-1 = " << i << endl;
                pthread_mutex_unlock(&t->DCTBuf->mutex);
 
                cout << "i0 = " << i << endl;
                t->ProcessBlock((*t->DCTBuf->Regions)[i],0);          
                cout << "i1 = " << i << endl;
                t->ProcessBlock((*t->DCTBuf->Regions)[i],1);
                cout << "i2 = " << i << endl;
                t->ProcessBlock((*t->DCTBuf->Regions)[i],2);
 
            }else
            {
                pthread_mutex_unlock(&t->DCTBuf->mutex);
                break;
            }
    }
 
    return (void*) EXIT_SUCCESS;
}
 
 
 
////////////////////////////////////////////////////////////////////////////
/// \brief PthreadDCT::Exec
/// \param arg
///
void PthreadDCT::Exec(void *arg)
{
    cout << "PthreadDCT::Exec0 " << endl;
    imshow("PthreadDCT::Exec",*ImOr);
    cvWaitKey(1000);
    cout << "PthreadDCT::Exec1 " << endl;
    pthread_create(handle, NULL, ThrdProcess, (void*)(this));
}
 
 
void PthreadDCT::ProcessBlock(Block *B , int offset)
{
    static const int c1=1004 /*cos(pi/16)<<10*/, s1=200 /*sin(pi/16)<<10*/;
    static const int c3=851 /*cos(3pi/16)<<10*/, s3=569 /*sin(3pi/16)<<10*/;
    static const int r2c6=554 /*sqrt(2)*cos(6pi/16)<<10*/, r2s6=1337;
    static const int r2=181; /* sqrt(2)<<7 */
    int row,col;
 
    static int width = ImOr->rows;
    //static int height = ImOr->cols;
 
    int offsetX = B->X;
    int offsetY = B->Y;
 
    cout << "PthreadDCT::ProcessBlock0 " << endl;
 
    for(row=0;row<8;row++)
    {
 
        cout << "PthreadDCT::ProcessBlock0 row0 " << row << "offsetX "<< offsetX << " offsetY " << offsetY << endl;
        imshow("test",*ImOr);
 
        cvWaitKey(1000);
 
        cout << "value row %d " << (row+offsetX) + (0+offsetY)*(nChannels*width) + offset<< endl;
        int x0=ImOr->data[(row+offsetX) + (0+offsetY)*(nChannels*width) + offset],
                x1=ImOr->data[(row+offsetX) + (1+offsetY)*(nChannels*width) + offset],
                x2=ImOr->data[(row+offsetX) + (2+offsetY)*(nChannels*width) + offset],
                x3=ImOr->data[(row+offsetX) + (3+offsetY)*(nChannels*width) + offset],
                x4=ImOr->data[(row+offsetX) + (4+offsetY)*(nChannels*width) + offset],
                x5=ImOr->data[(row+offsetX) + (5+offsetY)*(nChannels*width) + offset],
                x6=ImOr->data[(row+offsetX) + (6+offsetY)*(nChannels*width) + offset],
                x7=ImOr->data[(row+offsetX) + (7+offsetY)*(nChannels*width) + offset],
                x8;
        /* Stage 1 */
        x8=x7+x0; x0-=x7; x7=x1+x6; x1-=x6; x6=x2+x5; x2-=x5; x5=x3+x4; x3-=x4;
 
 
        cout << "PthreadDCT::ProcessBlock0 row1 " << row << endl;
 
        /* Stage 2 */
        x4=x8+x5; x8-=x5; x5=x7+x6; x7-=x6;
        x6=c1*(x1+x2); x2=(-s1-c1)*x2+x6; x1=(s1-c1)*x1+x6;
        x6=c3*(x0+x3); x3=(-s3-c3)*x3+x6; x0=(s3-c3)*x0+x6;
 
 
 
        /* Stage 3 */
        x6=x4+x5; x4-=x5; x5=x0+x2;x0-=x2; x2=x3+x1; x3-=x1;
        x1=r2c6*(x7+x8); x7=(-r2s6-r2c6)*x7+x1; x8=(r2s6-r2c6)*x8+x1;
 
 
        /* Stage 4 and output */
        //        dctBlock[row][0]=x6;
        //        dctBlock[row][4]=x4;
        //        dctBlock[row][2]=x8>>10;
        //        dctBlock[row][6] = x7>>10;
        //        dctBlock[row][7]=(x2-x5)>>10;
        //        dctBlock[row][1]=(x2+x5)>>10;
        //        dctBlock[row][3]=(x3*r2)>>17;
        //        dctBlock[row][5]=(x0*r2)>>17;
 
 
        B->tab[row + 8*0 +offset*64] = x6;
        B->tab[row + 8*4 +offset*64] = x4;
        B->tab[row + 8*2 +offset*64] = x8>>10;
        B->tab[row + 8*6 +offset*64] = x7>>10;
        B->tab[row + 8*7 +offset*64] = (x2-x5)>>10;
        B->tab[row + 8*1 +offset*64] = (x2+x5)>>10;
        B->tab[row + 8*3 +offset*64] = (x3*r2)>>17;
        B->tab[row + 8*5 +offset*64] = (x0*r2)>>17;
    }
 
    cout << "PthreadDCT::ProcessBlock1 " << endl;
 
    for(col=0;col<8;col++)
    {
//        int x0=dctBlock[0][col],
//                x1=dctBlock[1][col],
//                x2=dctBlock[2][col],
//                x3=dctBlock[3][col],
//                x4=dctBlock[4][col],
//                x5=dctBlock[5][col],
//                x6=dctBlock[6][col],
//                x7=dctBlock[7][col],
//                x8;
 
        int x0 = B->tab[0 + 8*col +offset*64],
                x1 = B->tab[1 + 8*col +offset*64],
                x2 = B->tab[2 + 8*col +offset*64],
                x3 = B->tab[3 + 8*col +offset*64],
                x4 = B->tab[4 + 8*col +offset*64],
                x5 = B->tab[5 + 8*col +offset*64],
                x6 = B->tab[6 + 8*col +offset*64],
                x7 = B->tab[7 + 8*col +offset*64],
                x8;
 
        /* Stage 1 */
        x8=x7+x0; x0-=x7; x7=x1+x6; x1-=x6; x6=x2+x5; x2-=x5; x5=x3+x4; x3-=x4;
 
 
        /* Stage 2 */
        x4=x8+x5; x8-=x5; x5=x7+x6; x7-=x6;
        x6=c1*(x1+x2); x2=(-s1-c1)*x2+x6; x1=(s1-c1)*x1+x6;
        x6=c3*(x0+x3); x3=(-s3-c3)*x3+x6; x0=(s3-c3)*x0+x6;
 
 
        /* Stage 3 */
        x6=x4+x5; x4-=x5; x5=x0+x2;x0-=x2; x2=x3+x1; x3-=x1;
        x1=r2c6*(x7+x8); x7=(-r2s6-r2c6)*x7+x1; x8=(r2s6-r2c6)*x8+x1;
 
 
        /* Stage 4 and output */
//        dctBlock[0][col]=(x6+16)>>5;
//        dctBlock[4][col]=(x4+16)>>5;
//        dctBlock[2][col]=(x8+16384)>>15;
//        dctBlock[6][col] = (x7+16384)>>15;
//        dctBlock[7][col]=(x2-x5+16384)>>15;
//        dctBlock[1][col]=(x2+x5+16384)>>15;
//        dctBlock[3][col]=((x3>>8)*r2+8192)>>14;
//        dctBlock[5][col]=((x0>>8)*r2+8192)>>14;
 
 
        B->tab[0 + 8*col +offset*64] =(x6+16)>>5;
        B->tab[4 + 8*col +offset*64] =(x4+16)>>5;
        B->tab[2 + 8*col +offset*64] =(x8+16384)>>15;
        B->tab[6 + 8*col +offset*64] = (x7+16384)>>15;
        B->tab[7 + 8*col +offset*64] =(x2-x5+16384)>>15;
        B->tab[1 + 8*col +offset*64] =(x2+x5+16384)>>15;
        B->tab[3 + 8*col +offset*64] =((x3>>8)*r2+8192)>>14;
        B->tab[5 + 8*col +offset*64] =((x0>>8)*r2+8192)>>14;
    }
 
}
 
 
 
//////////////////////////////////////////////////////
/////////////CLASS BufferDCT
 
 
/////////////////////////////////////////////////////
/// \brief BufferDCT
/// \param width
/// \param height
/// \param Block_size
///
BufferDCT::BufferDCT(int width,int height,int Block_size)
{
 
    mutex = PTHREAD_MUTEX_INITIALIZER;
    W = width;
    H = height;
 
    Regions = new vector <Block *>();
 
    for( int x=0; x<W; x+=Block_size)
        for( int y=0; y<H; y+=Block_size )
            Regions->push_back(new Block(x,y));
 
}
Et ma classe pThreadcpp

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 
/*
#########################################################################
#
#  This file is part of trustyRC.
#
#  trustyRC, fully modular IRC robot 
#  Copyright (C) 2006-2008 Nicoleau Fabien 
#
#  trustyRC is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  trustyRC is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with trustyRC.  If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
*/
 
/** @file pthread.h
 * @brief PThread header file
 */
 
#ifndef PTHREAD_H
#define PTHREAD_H
 
#include <pthread.h>
 
 
class PThread
{
protected:
    //virtual void*  Process(void *) = 0;  // la méthode à implémenter dans la classe enfant
    PThread();
    ~PThread();
 
    /// pthread handle
    pthread_t* handle;
    /// running status
    bool running;
    /// finished status
    bool finished;
    /// threaded function
 
public:
 
 
    /// Check if the thread is running
    bool IsRunning();
    /// Check if the thread is finished
    bool IsFinished();
    /// Join thread
    void* Join();
 
};
 
 
#endif
 
#include <pthreadcpp.h>
 
 
////////////////////////////////////////////////////
/// \brief PThread::PThread
///
PThread::PThread()
{
    this->finished = false;
    this->running = false;
    this->handle = new pthread_t;
}
 
 
////////////////////////////////////////////////////
/// \brief PThread::~PThread
///
PThread::~PThread()
{
    //delete handle;
}
 
 
////////////////////////////////////////////////////
/// \brief PThread::IsRunning
/// \return
///
bool PThread::IsRunning() {
    return this->running;
}
 
////////////////////////////////////////////////////
/// \brief PThread::IsFinished
/// \return
///
bool PThread::IsFinished() {
    return this->finished;
}
 
 
////////////////////////////////////////////////////
/// \brief PThread::join
/// \return
///
void* PThread::Join() {
   void* ret;
   pthread_join(*this->handle,&ret);
   return ret;
}

Donc je crée mon objet DCT et je lui affecte une image de ma webcam. Je lance la procedure Process de ma DCT qui crée un thread de traitement dct et j'execute cette thread.

Je problème se passe dans la donction ThrdProcess, j'essaye d'accèder à l'image ImOr que j'ai acquis grace à la l'argument qui est un pointer vers un PthreadDCT:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
void * ThrdProcess(void *arg)
{
    PthreadDCT *Thrd = (PthreadDCT *)arg;
    if(Thrd != NULL &&  !Thrd->IsRunning() )
    {
        Thrd->running   = true;
        Thrd->finished  = false;
        cout << "ThrdProcess(void *arg)0" << endl;
        cout << "Thrd->nChannels " << Thrd->nChannels << endl;
        imshow("ThrdProcess",*(Thrd->ImOr)); /// ERREUR!!!!!!!!!!!!!!!!!
        cout << "ThrdProcess(void *arg)1" << endl;
        while(1)
        {
            cvWaitKey(10);
        }
        Thrd->Process(arg) ;
 
        Thrd->running   = false;
        Thrd->finished  = true;
    }
 
}
Voici ma fonction main :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 
#include <pthreadcpp.h>
#include <dct.h>
 
int main(int argc,char **argv)
{
    VideoCapture cap(0);
 
    cout << "main0" << endl;
    Mat Im;
    cap >> Im;
 
    cout << "main1" << endl;
    DCT D(Im);
 
    cout << "main2" << endl;
    D.Process();
 
    return EXIT_SUCCESS;
}
J'arrive bien à accèder à l'image dans le pthreaddct mais dès que je passe dans la thread à proprement dite je n'ai plus l'accès à l'image. Pourtant j'ai bien mis la fonction void * ThrdProcess(void *arg) en friend de la classe pthreaddct ??


Quelqu'un aurait-il une idée de la raison pour laquelle je n'arrive plus a accéder à l'image dans la thread?

D'avance merci.