Synchronization of threads in Linux

Hello

I am trying to write a program to process images. The aim is to process different parts of an image at the same time. So i am using multithreading.

Lets say the input image has 1000 rows. Each thread is supposed to process 100 rows at one time. So 10 threads would do the job. But i want to have 3 threads working at the same time. So i decided to use JOINABLE threads. And with pthread_join i wait for each 3 threads to finish their jobs. Then i call the other three threads.

Thread1 Active
Thread2 Active
Thread3 Active
Thread1 Done
Thread2 Done
Thread3 Done

Thread4 Active
Thread5 Active
Thread6 Active
Thread4 Done
Thread5 Done
Thread6 Done

so on...

It is working now. But my problem is I want to start a thread when a previous thread finishes its job, without waiting for the other two threads to finish their jobs.

something like this

Thread1 Active
Thread2 Active
Thread3 Active

Thread1 Done
Thread4 Active --->Thread4 doesnt wait for 2. and 3. to finish their jobs
Thread2 Done
Thread3 Done

Thread4 Done


How can I achieve this????



I create and run my threads like this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
totalNumberThread = 10;
activeThreadNumber = 3;

pthread_t* thread = new pthread_t[activeThreadNumber];
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

for(int i=0; i<(int)ceil(totalNumberThread/activeThreadNumber); i++){
     for(int t=0; t<activeThreadNumber; t++){
         pthread_create( &thread[t], &attr, threadRun, this);
     }
     for(int t=0; t<activeThreadNumber; t++){
         pthread_join( thread[t], &status);
     }
}

void *threadRun(void *arg){

%%% Here startLine and endLine are defined  depending on the thread number %%%%

processImage(startLine,endLine);
pthread_exit(NULL);

}


I hope it is understandable.

Thanks
Kenter

Last edited on
> How can I achieve this????

Use a counting semaphore?
Thanks a lot. you solved my problem with 2 words!

cheers
Topic archived. No new replies allowed.