Trying to understand semaphore
-
@dheerendra actually i got both the values from both the thread but the value was only the first value then the next value were not updated donno whether my for loop is running?
-
@dheerendra
My thread Code:void add_thread::run() { int b = 0; int c = 0; qDebug("ADD_"); for (int a = 30; a < 40;++a) { addSem.acquire(); c = a + b; qDebug("ADD_DATA %d",c); disSem.release(); sleep(1000); b = c; addSem.available(); emit ADD_Data(c); sleep(1000); } qDebug("ADD_DATA %d",c); } My Other thread code: void display_thread::run() { int b = 0; int c = 0; for (int a = 10; a < 20; ++a) { addSem.acquire(); c = a + b; addSem.release(); b = c; addSem.available(); emit ADD_Data1(c); sleep(1000); } }
-
@ManiRon said in Trying to understand semaphore:
disSem.release();
My guess is right. Still you are releasing someother semaphore. Replace above line with addSem.release. Both the threads should work with same semaphore.
addSem.aquire() // do something addSem.release()
-
@dheerendra
Sorry sir i changed and tried but the same result. But that too the second thread data i am able to display in line edit but the first thread data i am able to print using Qdebug but couldnt see the data in line edit -
Can you show how did you declare semaphore ?
-
QSemaphore addSem;
-
- addSem is not initialised with initial semaphore count
- sleep(..) it is seconds. So to complete the loop 10 times, it takes the 10*1000 seconds. So approximately you have to wait for 3 hrs to complete this loop:). So change your sleep to sleep(1).
-
@dheerendra
done sir its working -
cool man. If you want the detailed example look at git
-
@dheerendra
Ok Sir -
Now issue is resolved, you can move the issue to SOLVED state. Enjoy Qt Programming.