Answer the question
In order to leave comments, you need to log in
What is the correct way to process images in a third-party stream?
Good day, kittens! I am writing software that processes images in a third-party stream. Yes, yes, I know you can't do that, but that's how it's supposed to be. Surely someone knows how to do it thread-safely. Now everything works, but after several iterations it hangs and does not work further. Sample code:
This is how I create a thread:
DWORD id;
HANDLE thread = CreateThread(NULL, 0U, MainThread, (LPVOID)td1,THREAD_PRIORITY_NORMAL, &id);
CloseHandle(thread);
Vcl::Graphics::TBitmap *bmp1 = new Vcl::Graphics::TBitmap();
Vcl::Graphics::TBitmap *bmp2 = new Vcl::Graphics::TBitmap();
TPngImage *png = new TPngImage();
TJPEGImage *jpeg = new TJPEGImage();
Rxgif::TGIFImage *gif = new Rxgif::TGIFImage();
TPicture *pic = new TPicture();
TPicture *picTrash = new TPicture();
bmp1->Canvas->Lock();
bmp2->Canvas->Lock();
bmp2->Canvas->FillRect(bmp2->Canvas->ClipRect);
bmp2->Canvas->Draw(0,0,bmp1);
bmp2->Canvas->Unlock();
bmp1->Canvas->Unlock();
Answer the question
In order to leave comments, you need to log in
What I see so far.
1. It is FORBIDDEN to use CreateThread in any high-level library, use beginthreadex (or something else provided by the library) instead. Or, since you're using the VCL in a black way, it won't hurt much if you use TThread.
2. Lock and Unlock is an ordinary mutex. All competing threads who want to draw on the canvas are waiting and smoking, doing nothing.
3. You missed the most important thing. If you are waiting for help, you don’t need to be so secret that you won’t understand what’s wrong. Where - at least approximately - is the body of the thread, and what is being done in the main thread?
4. How should you work? There is a certain data structure in which we return processed pictures from the stream, and volatile bool isWorking, which is responsible for whether the stream is working or not. When this flag is false, the main thread has the right to work with our structure. It switches to true - the structure can contain any garbage, it is forbidden to access it! If you also need to warn when the thread has ended and you can take information - TThread.Synchronize or PostMessage. Both work only in GUI programs, if console - then through events (CreateEvent).
5. And why “you can’t do this”? If the party ordered, then it is necessary. It’s just that multithreading is a difficult thing, errors are at every step, but “it’s impossible in principle” - there is no such thing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question