Click here to Skip to main content
15,901,666 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: HOWTO: get CMenu information from a CMenu pointer Pin
Mark Salsbery4-Nov-06 12:23
Mark Salsbery4-Nov-06 12:23 
GeneralRe: HOWTO: get CMenu information from a CMenu pointer Pin
Mohammad A Gdeisat4-Nov-06 19:50
Mohammad A Gdeisat4-Nov-06 19:50 
GeneralRe: HOWTO: get CMenu information from a CMenu pointer Pin
Mark Salsbery4-Nov-06 22:48
Mark Salsbery4-Nov-06 22:48 
QuestionAbout Multi Core Pin
bouli4-Nov-06 5:17
bouli4-Nov-06 5:17 
AnswerRe: About Multi Core Pin
Mark Salsbery4-Nov-06 8:42
Mark Salsbery4-Nov-06 8:42 
GeneralRe: About Multi Core Pin
bouli4-Nov-06 13:54
bouli4-Nov-06 13:54 
GeneralRe: About Multi Core Pin
Mark Salsbery4-Nov-06 16:07
Mark Salsbery4-Nov-06 16:07 
GeneralRe: About Multi Core Pin
bouli5-Nov-06 0:28
bouli5-Nov-06 0:28 
Sure Smile | :)

Random header:

#pragma once

class CRandomDraw
{
public:
CRandomDraw();
~CRandomDraw();

void Start();
void Stop();

void SetCpu(int nCpu);
void SetHwnd(HWND hWnd);
void SetRect(RECT rect);

private:
BOOL m_bRunning;
DWORD m_dwMask;
HWND m_hWnd;
HDC m_hDC;
HANDLE m_hThread;
HANDLE m_hEvent;
RECT m_rect;

int m_nCurrentCpu;

static DWORD WINAPI RandomThread(LPVOID lpParameter);
};

Random implementation (cpp):
#include "StdAfx.h"
#include "RandomDraw.h"
#include <math.h>

CRandomDraw::CRandomDraw()
{
m_bRunning=FALSE;
m_hDC=NULL;
m_hThread=NULL;
m_hEvent=CreateEvent(NULL, TRUE, TRUE, NULL);
m_nCurrentCpu=0;
m_dwMask=1;

srand((int) time(NULL));
}

CRandomDraw::~CRandomDraw()
{
Stop();

::CloseHandle(m_hEvent);
m_hEvent=NULL;
}

void CRandomDraw::SetHwnd(HWND hWnd)
{
m_hWnd=hWnd;
m_hDC=::GetDC(m_hWnd);
}

void CRandomDraw::SetRect(RECT rect)
{
m_rect=rect;
}

void CRandomDraw::SetCpu(int nCpu)
{
m_nCurrentCpu=nCpu;
}

void CRandomDraw::Start()
{
if (m_bRunning)
return;

m_bRunning=TRUE;

m_hThread=::CreateThread(NULL, 0, RandomThread, this, CREATE_SUSPENDED, NULL);

m_dwMask=(DWORD) pow(2, (double) m_nCurrentCpu);

::SetThreadAffinityMask(m_hThread, m_dwMask);

::ResumeThread(m_hThread);
}

void CRandomDraw::Stop()
{
if (!m_bRunning || !m_hThread)
return;

m_bRunning=FALSE;

WaitForSingleObject(m_hEvent, INFINITE);
::CloseHandle(m_hThread);
m_hThread=NULL;
}

DWORD WINAPI CRandomDraw::RandomThread(LPVOID lpParameter)
{
CRandomDraw* pThis=reinterpret_cast<CRandomDraw*>(lpParameter);

while (pThis->m_bRunning)
{
::SetPixel(pThis->m_hDC, rand() % pThis->m_rect.right, rand() % pThis->m_rect.bottom, RGB(rand() % 255, rand() % 255, rand() % 255));

::Sleep(0);
}

SetEvent(pThis->m_hEvent);

return 0;
}

Calls in the SDI view header:
...
private:
CRandomDraw m_randomDraw[CPU_COUNT];

void StartAll();
void StopAll();
void SetHwndAll(HWND hWnd);
void SetRectAll(RECT rect);
afx_msg void OnDestroy();
...

Implementation in the SDI (cpp):
...
BOOL CRandomView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
BOOL bRet=CView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);

SetHwndAll(m_hWnd);
StartAll();

return bRet;
}
...
void CRandomView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
RECT rect;
GetWindowRect(&rect);

SetRectAll(rect);
}

void CRandomView::StartAll()
{
for (int i=0; i<CPU_COUNT; i++)
{
m_randomDraw[i].SetCpu(i);
m_randomDraw[i].Start();
}
}

void CRandomView::StopAll()
{
for (int i=0; i<CPU_COUNT; i++)
m_randomDraw[i].Stop();
}

void CRandomView::SetHwndAll(HWND hWnd)
{
for (int i=0; i<CPU_COUNT; i++)
m_randomDraw[i].SetHwnd(hWnd);
}

void CRandomView::SetRectAll(RECT rect)
{
for (int i=0; i<CPU_COUNT; i++)
m_randomDraw[i].SetRect(rect);
}

void CRandomView::OnDestroy()
{
StopAll();
CView::OnDestroy();

// TODO: Add your message handler code here
}
...

Maybe I have missed something.
As I said, normally, the pixels should be displayed CPU_COUNT faster because there are CPU_COUNT threads running on separate CPU, while each CPU runs 1 thread. So, 4 pixels at a time should be displayed. But it is much slower than running 1 thread. This is a paradox. And what I don't understand.
Thanks to help me to solve my problem Smile | :)

Best regards.

Fred.



There is no spoon.
GeneralRe: About Multi Core Pin
Mark Salsbery5-Nov-06 8:14
Mark Salsbery5-Nov-06 8:14 
GeneralRe: About Multi Core Pin
bouli5-Nov-06 8:21
bouli5-Nov-06 8:21 
GeneralRe: About Multi Core Pin
Mark Salsbery5-Nov-06 8:53
Mark Salsbery5-Nov-06 8:53 
AnswerRe: About Multi Core Pin
Mark Salsbery4-Nov-06 8:57
Mark Salsbery4-Nov-06 8:57 
QuestionAccess control from a user defined class (CFormView based SDI application) [modified] Pin
cy163@hotmail.com4-Nov-06 4:55
cy163@hotmail.com4-Nov-06 4:55 
AnswerRe: Access control from a user defined class (CFormView based SDI application) Pin
Mark Salsbery4-Nov-06 8:19
Mark Salsbery4-Nov-06 8:19 
GeneralRe: Access control from a user defined class (CFormView based SDI application) Pin
cy163@hotmail.com5-Nov-06 2:39
cy163@hotmail.com5-Nov-06 2:39 
GeneralRe: Access control from a user defined class (CFormView based SDI application) Pin
cy163@hotmail.com5-Nov-06 3:52
cy163@hotmail.com5-Nov-06 3:52 
GeneralRe: Access control from a user defined class (CFormView based SDI application) Pin
Mark Salsbery5-Nov-06 7:38
Mark Salsbery5-Nov-06 7:38 
GeneralRe: Access control from a user defined class (CFormView based SDI application) [modified] Pin
cy163@hotmail.com5-Nov-06 18:27
cy163@hotmail.com5-Nov-06 18:27 
QuestionHow to create a child window with menu from a dialog based application? Pin
Cyber Friend4-Nov-06 4:52
Cyber Friend4-Nov-06 4:52 
QuestionReallocation Pin
hint_544-Nov-06 3:53
hint_544-Nov-06 3:53 
AnswerRe: Reallocation Pin
Waldermort4-Nov-06 4:55
Waldermort4-Nov-06 4:55 
GeneralRe: Reallocation Pin
hint_544-Nov-06 5:01
hint_544-Nov-06 5:01 
AnswerRe: Reallocation Pin
Michael Dunn4-Nov-06 10:29
sitebuilderMichael Dunn4-Nov-06 10:29 
Newshelp Pin
aliheydari4-Nov-06 2:27
aliheydari4-Nov-06 2:27 
GeneralRe: help Pin
George L. Jackson4-Nov-06 3:43
George L. Jackson4-Nov-06 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.