官方淘宝店 易迪拓培训 旧站入口
首页 > 手机设计 > 手机测试交流 > 请教多线程的有关知识

请教多线程的有关知识

05-08
我现在程序里面有三个线程,一个是显示线程,两个是测试线程A和B,就是在初始化的时候让示线程和A 测试进行,B挂起,当A执行到67秒的时候需要让B手机开机,在B手机开机的同时A手机需要,等待B开机,当B手机开机后A继续进行,当A运行完后挂起,B进行到67秒后让A手机开机,过程跟B一起,如此循环,这这是一个手机的测试系统,怎么样来控制A。B之间的通信?请高人指导。非常感谢!
以下是我写的程序,高人知道下。
#include "stdafx.h"
#include "E1602.h"
#include "E1602Dlg.h"
#include "MathThread.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
int flag1=1,flag2=0;
CMTThread1 *p1;
CSQThread *p2;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CE1602Dlg dialog
CE1602Dlg::CE1602Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CE1602Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CE1602Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CE1602Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CE1602Dlg)
DDX_Control(pDX, IDC_LIST2, m_mtList);
DDX_Control(pDX, IDC_LIST1, m_sqList);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CE1602Dlg, CDialog)
//{{AFX_MSG_MAP(CE1602Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CE1602Dlg message handlers
BOOL CE1602Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}
void CE1602Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CE1602Dlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CE1602Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CE1602Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
CMTThread1 *pmtt = (CMTThread1 *)AfxBeginThread(RUNTIME_CLASS(CMTThread1),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);
pmtt->SetOwner(this);
pmtt->ResumeThread();
CSQThread* psqt=(CSQThread*)AfxBeginThread(RUNTIME_CLASS(CSQThread),
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);
psqt->SetOwner(this);
psqt->ResumeThread();
p1=pmtt;
p2=psqt;
while(1)
{
if(flag1)
{
psqt->SuspendThread();
pmtt->ResumeThread();
Sleep(6000);
::AfxMessageBox("Turn on B");
pmtt->SuspendThread();
Sleep(3000);
//pmtt->ResumeThread();
flag2=1;
flag1=0;
}
if(flag2)
{
pmtt->SuspendThread();
psqt->ResumeThread();
Sleep(6000);
::AfxMessageBox("Turn on A");
Sleep(3000);
flag2=0;
flag1=1;

}
}
}
void CE1602Dlg::OnButton2()
{
// TODO: Add your control notification handler code
//p1->SuspendThread();
//p2->SuspendThread();
}
new&boardid=71

这是多线程的同步问题,用信号量、临界区、事件都可以解决这个问题。
你在MSDN中找一下这几个关键字,还有示例程序的:
CEvent
CCriticalSection
CMutex
WaitForSingleObject

Top