Sharing data between Program1 and Program2 with QSharedMemory
-
Hello, I have a simple question about QSharedMemory
I have 2 totally separated program like Program1 and Program2
-------------------------------------------------- here is a code " Program1 "
#include <QSharedMemory>key="SharedMemory_Test"; sharedMemory=new QSharedMemory(key); while(true) { Sleep(3000); if(sharedMemory->attach()) { sharedMemory->lock(); sharedMemory->unlock(); sharedMemory->detach(); std::cout << "[1] detach done ! " << std::endl; } else { std::cout << "[2] waitting !" << std::endl; }
----------------------------------------------- here is a code " Program2 "
#include <QSharedMemory>key="SharedMemory_Test"; sharedMemory=new QSharedMemory(key); while(true) { Sleep(3000); std::cout << "[01] : " << sharedMemory->attach() << std::endl; std::cout << "[02] : " << sharedMemory->isAttached() << std::endl; if(sharedMemory->isAttached() == 0) { sharedMemory->create(1); std::cout << "[1] create done !" << std::endl; sharedMemory->lock(); sharedMemory->unlock(); } else { std::cout << "[2] waitting !" << std::endl; }
------------------------------------------------------------------code end
[Question]
If i run Program1 first, of course it keeps print " [2] waitting ! " because I don't execute Program2 yet. and then I run Program2 during Program1 is keep running. Program1 shows me the print " [1] detach done ! " but it dosen't stop even it did detach at the same time.if you know why please let me know !
-
@donkey007 said in Sharing data between Program1 and Program2 with QSharedMemory:
but it dosen't stop even it did detach at the same time
Why should it stop if you have no condition to leave the endless loop?
You should not use such loops in event driven applications like Qt applications.
If you want to check something or do something periodically in a proper way use QTimer. -
@jsulm Thanks for your comment !
But I want to check "synchronization" between Program1 and Program2
I thought Program2 creates sharedmemory then Program1 condition(" if(sharedMemory->attach()) ") return true and
" sharedMemory->detach(); " too. so If Program1 detach sharedMemory then " if(sharedMemory->attach()) " return false.If its correct, Program1 should print "[1] detach done ! " only one time and then print "[2] waitting !" until Program2 create sharedmemory agrain. Is it wrong?
Its my guess.------------------------
- Program1 start
- Program1's (sharedMemory->attach() ) return "FALSE"
- so its print "[2] waitting !"
- Program2 start during Program1 running
- Program2's (sharedMemory->isAttached()) return "FALSE"
- so it's create sharedMemory and then print "[1] create done !" only one time and print "[2] waitting !" until Program1 detach sharedMemory
end --------------------------------------------------
But my problem is Program1 keep print "[1] detach done ! " even it execute detach.
Is detach not make "sharedMemory->attach()" condition to FALSE ?
-
@donkey007 You are confusing attach() and isAttached().
Your Program1 is not calling isAttached(). Once the shared memory is created by Program2, Program1 just attaches then immediately detaches, each time around the loop.
-
@KenAppleby-0 Thanks for your comment !
So, is there any way to make sync between Program1 and Program2 ?
I want to make Program2 send data and wait until Program1 read the data.
It means i wnat to make sure Program1 get the correct sequential data(like 1 ~ 10 frame without lost) like camera image.please let me know
-
@donkey007 said in Sharing data between Program1 and Program2 with QSharedMemory:
So, is there any way to make sync between Program1 and Program2 ?
Shared memory wouldn't be much use without such a thing :-) The lock and unlock functions are synchronisation functions of a sort, but you would have to use a polling pattern.
QSystemSemaphore probably does what you need.
Are you sure you need separate processes and not just separate threads in one process?
-
@KenAppleby-0 Yes sir...
In my case I need separate processes becauseProgram2(sender, c++) give camera image to Program1(receiver, python wrapper with c++) and Program1 detect/track the object then Program1 give back tracking results to Program2 finally Program2 draw bounding box with tracking results from Program1.
I already made this but Program1 not getting whole frame image like 1->2->X->4->5->6
It makes bounding box showing delay. -
@donkey007 said in Sharing data between Program1 and Program2 with QSharedMemory:
It makes bounding box showing delay.
Not sure which/what "delay" you are talking about. But as long as you poll with
Sleep(3000)
you are liable to have up to a 3 second delay, so this pattern is not going to be acceptable (let alone any "delay" the OS may impose swapping between processes if time is critical).Unless you set the "sleep" really low, which risks making the waiting process "busy", polling will be a problem. If you want the fastest response @KenAppleby-0 suggestion of a semaphore is probably the best way to go.