English 中文(简体)
改变在窗 the中写成的法典,以 w子
原标题:Converting the code written in windows threads to wxwidgets threads

I have the code that makes use of multithreading however it is via using windows i want to convert the code in wxwdigets i have been trying for a long time but there is no success and ultimately i deleted what i did and wants to start from scratch

#include "windows.h"
#include <iostream>
#include <stdio.h>


DWORD WINAPI ThreadFn(LPVOID vpParam);

int main() {
    using namespace std;

    // Create the thread and pass in the function pointer and counter
    unsigned int uiCounter = 0;
    DWORD qThreadID;
    HANDLE hThread = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);

    // Loop until the user enters  q 
    char cChar =    ;
    while (cChar !=  q ) {
        cout << uiCounter << endl;
        cChar = (char)getchar();
    }

    // Close the handle to the thread
    CloseHandle(hThread);

    return 0;
}


DWORD WINAPI ThreadFn(LPVOID vpParam)
{
    unsigned int& uirCounter = *((unsigned int*)vpParam);
    // Increment up to the maximum value
    while (uirCounter < 0xFFFFFFFF) {
        ++uirCounter;
    }
    return 0;
}
问题回答

你颁布的法典存在几个严重问题。 在你做任何事情之前,他们必须固定下来。

  1. The worker thread is a busy loop . It will consume all the available CPU cycles and make anything else, such as running a GUI, impossible slow and unresponsive. I suggest adding a call to Sleep() into the while loop, as a quick fix.

  2. 主要的透镜和工人都试图同时获得对应变量。 你走了这么长的时间,而且会失事。 你们应当保护反.或等同。

让我们稍作重新设计,并去种植牛肉。 我们需要

A. 导 言 一位工人在校服,第二次(大约)10次,在休息时间睡觉,以便我们能够更新《两性平等倡议》。

B. A GUI thread which checks and displays the counter value twice per second

C. 出席情况 如果q升,GUI透镜还将监测关键板,停止反面。

D. 出席情况 我们将使用wxThreadHelper。 • 管理深层的班子。

主要执行细节如下:

2. 界定主要框架,包括所有轮 the良好

class MyFrame : public wxFrame, public wxThreadHelper
{
public:
    MyFrame(const wxString& title);

    // worker thread entry point
    wxThread::ExitCode Entry();

    // keyboard monitor
    void OnChar(wxKeyEvent& event );

    // display updater
    void OnTimer(wxTimerEvent& event);

private:

    // the counter
    unsigned int mCounter;

    // protect the counter from access by both threads at once
    wxCriticalSection mCS;

    // display update timer
    wxTimer * mTimer;

    DECLARE_EVENT_TABLE()
};

观察主要板和时间活动

BEGIN_EVENT_TABLE(MyFrame, wxFrame)
    EVT_CHAR(MyFrame::OnChar)
    EVT_TIMER(-1,MyFrame::OnTimer)
END_EVENT_TABLE()

构筑框架

MyFrame::MyFrame(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title)
       , mCounter( 0 )
{
    // set the frame icon
    SetIcon(wxICON(sample));


    CreateStatusBar(2);
    SetStatusText("Welcome to Asynchronous Counter!");

    // Create thread and start it going

    CreateThread();
    GetThread()->Run();

   // Create update timer and start it going

    mTimer = new wxTimer(this);
    mTimer->Start(500);
}

工人读写能力每秒10倍

 wxThread::ExitCode MyFrame::Entry()
 {
     // loop, so long as we haven t been asked to quit
    while ( ! GetThread()->TestDestroy()) {
        {
            // increment counter, to a maximum value
             wxCriticalSectionLocker lock( mCS );
             if( mCounter >= 0xFFFFFFFF )
                 break;
            ++mCounter;
        }

        // Let other things happen for 1/10 second
        Sleep(100);

    }

    // Counter is finished, or we have been asked to stop
    return (wxThread::ExitCode)0;

 }

发出要求的关键监测板

 void MyFrame::OnChar(wxKeyEvent& event )
    {
        event.Skip();
        if( event.GetKeyCode() == (int)  q  )
            GetThread()->Delete();
    }

B. 时间火力时的最新显示

void MyFrame::OnTimer(wxTimerEvent& )
{
    unsigned int current_counter_value;
    {
         wxCriticalSectionLocker lock( mCS );
        current_counter_value = mCounter;
    }
    SetStatusText(wxString::Format("Counter = %d",current_counter_value));
}




相关问题
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?

热门标签