English 中文(简体)
具有多功能成员的班级
原标题:class with Multithreaded member function

我有一个班子试图改变其某些成员的职能,以适应不同的线索。 虽然该方案没有问题,但它在试图从图像缓冲(由不同的透镜更新)中读时坠毁。 如同问题一样,在争论不正确的情况下,就会产生问题。

下面的《法典》应当更明确地解释我试图做些什么。 基本上,我所要完成的是,成员的职能“填充”填补了形象的缓冲,而其他方案正在做其他事情,包括同时阅读同样的图像缓冲。

非常感谢对辛迪加的任何帮助。

 const int MaxImgBufferSize = 5;
        class MyFrame : public wxFrame
        {
        public:
            // constructors
            MyFrame(const wxString& title);   

        private:
            static vector <IplImage*> ImgBuffer;
            void  changeWP(wxCommandEvent&);
            void  fillBuffer();
                void  fillBufferFun();
                static void __cdecl getImgFromBuffer(void *);
                static void __cdecl pushImgBuffer(void *);
        };

    vector<IplImage*> MyFrame::ImgBuffer;

        enter code here

    MyFrame::MyFrame(const wxString& title)
           : wxFrame(...)
    {
       // some stuff here
       InitializeCriticalSection(&Section);
       fillBuffer();

      // some code here calls changeWP(wxCommandEvent&) from time to time
    }

    void MyFrame::fillBuffer()
    {
        while(ImgBuffer.size() <= MaxImgBufferSize)
        {
            fillBufferFun();
        }
    }

void MyFrame::fillBufferFun()
{
   ImgBuffer* img;
   // do something with img
   _beginthread(pushImgBuffer, 0, img);
}

void MyFrame::pushImgBuffer(void *p)
{
    EnterCriticalSection(&Section);
    ImgBuffer.push_back( (IplImage*) p );
    LeaveCriticalSection(&Section);
}

static unsigned int __stdcall getImgFromBuffer(void *);

void MyFrame::changeWP(wxCommandEvent&)
{
    // do someting

    IplImage* img = NULL;// new IplImage;
        _beginthreadex( NULL, 0, MyFrame::getImgFromBuffer, img, 0, NULL );

        // do something with img
        fillBuffer();
}

unsigned int MyFrame::getImgFromBuffer(void *p)
{
    EnterCriticalSection(&Section);
    p = (void *)ImgBuffer[0];
    ImgBuffer.erase(ImgBuffer.begin());
    LeaveCriticalSection(&Section);
    return 0;
}
问题回答

这里有几个问题:

  1. Your code is crashing because your getImgFromBuffer function doesn t have the effect you seem to intend for it. It seems, from looking at the body of getImgFromBuffer, that you are trying to copy a pointer value out of the vector (MyFrame::ImgBuffer) and use it overwrite the pointer value passed in from the function that invoked it (i.e., the "img" variable inside MyFrame::changeWP). I say "seems" because of your initialization of the "img" variable inside MyFrame::changeWP (IplImage* img = new IplImage) just before you invoke getImgFromBuffer -- why assign a new object to the pointer just before assigning yet another, different pointer value to it (leading, of course, to a memory leak)? Anyhow, the assignment is currently just overwriting the value of the "p" argument inside the getImgFromBuffer function, which was passed by value and will be lost when the function exits. If you want getImgFromBuffer to overwrite a pointer variable passed in from its caller, then you need to pass a pointer to the pointer variable, like so:
void MyFrame::changeWP(wxCommandEvent&)
{
    IplImage* img = NULL;  // No memory leak, this time.
    _beginthread(MyFrame::getImgFromBuffer, 0, & img);
    //...
}

void MyFrame::getImgFromBuffer(void ** p)
{
    //...
    *p = (void *)ImgBuffer[0];
    //...
}
  1. I don t see how "Section" is defined, but whether it s static or instance, it s at least bad form either way. If it s static, then you re making the mistake of re-initializing it every time you construct. I realize that this a class representing a top-level window, so you probably won t be making more than one of them, but it s still bad form. If Section is an instance variable, then you could have multiple Section objects trying to protect a single (static) resource, with no mutual exclusion between them. Again, there s only the one window, so this probably isn t your actual problem, but still...

也许会更多,但这足以开始。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签