|
manustone wrote: Zac just told me that there is a Win32 (not MFC) that does it. My intention was to wait an amount of time calling Sleep and stopping for a while the calling thread.
A couple things here: First, anytime you are calling Sleep, you really should rethink your design. That is one function that should almost never be called; especially as a thread synchronization technique. Second, you really should pick up "Programming Windows" by Charles Petzold and "Programming Windows with MFC" by Jeff Prosise. You are apparently under the impression that MFC is different than Win32 when in fact, MFC is nothing more than a thin wrapper around the Win32 API.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thank you very much for the advice!
|
|
|
|
|
Woops!
Jun..I forgot to really thank you for the help!!!
bye MANU
|
|
|
|
|
You're welcome. Hope you got all the information you were asking for.
Best,
Jun
|
|
|
|
|
Problem 1: Optimization
A C++ class has the following member:
int status_[75];
During performance analysis it was found out that the class (its object) was performing unsatisfactorily
making it a necessary candidate for optimization.
The following additional information was also found out
*) Each element of the status_ array assumes only the following 4 values: -1, 0, 1, 2
*) The bulk of the time was spent in loops like:
for(size_t i=0; i != 75; ++i)
{
if(status_[i] == 1)
{
// do something...
}
}
*) The set of elements of the status_ array that assume a particular value is sparse making the above loop
inefficient.
Assume that 0 is the default value.
*) The status_ array is updated at places scattered throughout the code making it difficult to rewrite. However, a
particular element is updated only through a statement like
status[i] = -1;
Task: Your task is to optimize the class, considering the above
information.
The optimization should introduce the least bugs possible, i.e. it must
have
minimal changes and those changes that have lesser chance of
introducing bugs.
Never Ending Probloem
|
|
|
|
|
read this[^] before asking what you do
|
|
|
|
|
Again, asking people to answer these questions for you does not help you, and does not help the company that is interviewing you get a proper evaluation of your skill level.
At the very least, you should make an attempt prior to asking for help!
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Indian Idol wrote: Your task...
Not ours.
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
This is not my interview questions. I saw this question somewhere and
I wanted to increase my Academic knowledge.
Noting related to homework.
I have some solutions just wanted to verify and hoping to get a better solution.
|
|
|
|
|
-- TTD --
|
|
|
|
|
Problem 2: Threading:
Requirements: In a particular system there are two threads T1 and T2.
T1 is the master thread that periodically sends messages to the worker
thread T2.
The system is time-critical and hence T2 must react to every message
that T1 sends.
None of the messages must be lost.T2 performs time consuming operations
like say mixing sound samples and sending them to the sound buffer.
Here is the hypothetical POSIX like library that is available to synchronize the threads:
Mutex locking and unlocking are done through to the calls
int lock(Mutex_t* mutex);
int unlock(Mutex_t* mutex);
Thread messaging is done through condition variables that operate
with an associated mutex.
A thread that responds to a message needs to wait on a condition variable.
It should own the associated mutex prior to entering the wait state.
When it enters the wait state, it simultaneously releases the ownership
of the mutex. Upon return from the wait state, it owns the mutex.
int wait(Condition_t* condition, Mutex_t* mutex);
A thread that sends message to another thread needs to signal the condition
variable. In order to signal the condition variable, the thread must own the
associated mutex.If no thread is waiting on the condition variable, the `signal'
or message is lost. If a thread is waiting on the condition variable, then it resumes
execution.
int signal(Condition_t* condition, Mutex_t* mutex);
Task: You can write this in pseudo code. The idea is to
come up with the best and efficient algorithm that would address the
requirements.
Search Not Complete
|
|
|
|
|
are you asking people to answer your interview questions for you ?
|
|
|
|
|
This is not my interview questions. I saw this question somewhere and
I wanted to increase my Academic knowledge.
|
|
|
|
|
Since you don't have the answer, I take it that you are interviewing for a position ... in which case, answering the question for you does no good for either you nor the company you are applying to work for. Pick up an Intro to Multithreading book, or at the very least try to come up with a solution yourself before asking for help.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
If you would have done your homeworks yourself when you were at school, you would be able to answer the interview questions
|
|
|
|
|
Cedric Moonen wrote: If you would have done your homeworks yourself when you were at school, you would be able to answer the interview questions
1 point !
Cedric Moonen wrote: Charting control [Updated - v1.1]
hum hummm
|
|
|
|
|
This is not my interview questions. I saw this question somewhere and
I wanted to increase my Academic knowledge.
Noting related to homework.
|
|
|
|
|
Hi all,
I need to generate and display a GUID everytime the user is clicking a button. I am using MFC and its a dialog based application. How can I do this?
Aljechin.
|
|
|
|
|
void CMyCoolDialog::OnButtonGenerateGUID()
{
GUID guid;
::CoCreateGuid(&guid);
}
Ovidiu Cucu
Microsoft MVP - Visual C++
|
|
|
|
|
From one of my client applications, I open non-blocking TCP sockets to multiple servers and then immediately call select() on all these sockets with a time out value of 30 seconds. The select() always returns before timeout value and its value is always less than the available servers.
For example I open 3 non blocking TCP connections for connecting to 3 up and running servers on same machine. Then I call select() on these 3 sockets. The return value of select() is 2 rather than 3. Even the logs of 3 servers shows connection attempts.
Following is code snippets:
SOCKET fds[MAX_SOCKETS];
struct sockaddr_in ads [MAX_SOCKETS];
for (int nNumConnections = 0; nNumConnections < 3; nNumConnections++)
{
ads[nNumConnections].sin_addr.s_addr = inet_addr(lpszIpAddr);
ads[nNumConnections].sin_family = AF_INET;
ads[nNumConnections].sin_port = htons((unsigned short) (nPort[nNumConnections]) );
}
fd_set writeFDS,exceptFDS;
FD_ZERO (&writeFDS);
FD_ZERO (&exceptFDS);
int nMaxFd = 0;
int nSocketCount = 0;
for (int i = 0; i < 3; i++)
{
fds[i] = socket( AF_INET, SOCK_STREAM , IPPROTO_TCP );
unsigned long setBlocking = 1;
if ((ioctlsocket (fds[i], FIONBIO, &setBlocking)) != 0)
{
return -1;
}
int nResult = connect(fds[i], (SOCKCONST sockaddr *) &ads[i], sizeof (ads[i]));
if (nResult == 0)
{
if (fds[i] >= nMaxFd) nMaxFd = fds[i] + 1;
FD_SET (fds[i], &writeFDS);
FD_SET (fds[i], &exceptFDS);
nSocketCount ++
}
else if (nResult == SOCKET_ERROR)
{
if(WSAGetLastError() == WSAEWOULDBLOCK)
{
if (fds[i] >= nMaxFd) nMaxFd = fds[i] + 1;
FD_SET (fds[i], &writeFDS);
FD_SET (fds[i], &exceptFDS);
nSocketCount++;
}
}
}
cout << "Socket count in the FD_SET :" << nSocketCount;
if (nSocketCount > 0)
{
struct timeval tv;
tv.tv_sec = 60;
tv.tv_usec = 0;
nSelectStatus = select (nMaxFd, NULL, &writeFDS, &exceptFDS, &tv);
cout <<"Return value of select :" << nSelectStatus;
if (nSelectStatus <=0 || nSelectStatus == SOCKET_ERROR)
{
}
else
{
for (int i = 0; i < 3; i++)
{
if (FD_ISSET (fds[nIndex], &exceptFDS))
{
}
else if (FD_ISSET (fds[nIndex], &writeFDS))
{
}
}
}
}
return 0;
}
Output:
Socket count in the FD_SET :3
Return value of select :2 (sometime it comes 1 or 3)
I am not very clear the functionality of select() in case of multiple sockets.
Does it wait for all sockets to be ready OR return as soon as get some of the sockets running ?
-- modified at 0:06 Friday 1st September, 2006
|
|
|
|
|
first of all, edit your post (don't repost all, use the edit link), and put all your code into <pre></pre> tags, so that we can rean your question more clearly.
use also the < and > buttons for your for loops
|
|
|
|
|
Hi there!
I’m trying to use the CAnimateCtrl in a CDialog based class so that I can use it as a kind of CWaitCursor thing, that will show a “Please wait…” message an plays a AVI.
Now, since I would also like to use the “XP Theme”, I’ve defined a “manifest” as a resource, and as a result the AVI file will not play.
In order to make it work, I tried to use a thread as the one in the MSDN sample (CAnimateCtrl::CAnimateCtrl in MFC Library Reference).
The problem is when the CAnimateCtrl is being destroyed it takes too long (?), and it also makes the application crash. The MSDN says that "... If you create the CAnimateCtrl object on the stack, it is destroyed automatically."
Below you can see the most relevant (I think) parts of the code. Can someone please tell me what am I doing wrong?
Thanks in advance.
CWorkingDlg::CWorkingDlg( LPCSTR szLabel, bool bProgress, int nMaxProgress, bool bShowCancel, UINT nIdAVI, CWnd* pParent)
: CDialog(CWorkingDlg::IDD, pParent)
{
m_bCancelState = false;
m_dwThreadId = 0;
m_hThreadAvi = NULL;
m_hEventAvi = NULL;
m_bCancel = bShowCancel;
m_bProgress = bProgress;
m_nMaxProgress = nMaxProgress;
CString strLabel(szLabel);
if(strLabel.IsEmpty())
strLabel = "A processar. Aguarde por favor...";
m_strLabel = strLabel;
if (nIdAVI==0)
nIdAVI=IDR_XX_AVI_RODAS;
m_nAVI=nIdAVI;
EnableModal(pParent);
if(!Create(CWorkingDlg::IDD, pParent))
AfxThrowUserException();
ShowWindow(SW_SHOW);
DispatchMsg();
}
CWorkingDlg::~CWorkingDlg()
{
if (m_hThreadAvi)
{
::PostThreadMessage(m_dwThreadId, WM_STOPCLIP, 0L, 0L);
::WaitForSingleObject(m_hEventAvi, INFINITE);
m_hThreadAvi=NULL;
}
if (m_hEventAvi)
{
::CloseHandle(m_hEventAvi);
m_hEventAvi=NULL;
}
DisableModal(GetParent());
if (GetSafeHwnd())
DestroyWindow();
}
void CWorkingDlg::DispatchMsg()
{
MSG msg;
DWORD dwi=GetTickCount();
while (::PeekMessage(&msg, NULL, 0L, 0L, PM_NOREMOVE))
{
if (::GetMessage(&msg,NULL,0L,0L))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else
return;
if ((GetTickCount()-dwi)>2000) // 2s
return;
}
}
UINT CWorkingDlg::MyClipThreadProc(LPVOID pParam)
{
AviInfo *pInfo=(AviInfo *)pParam;
CWnd* pParentWnd = CWnd::FromHandle(pInfo->hWndParent);
CRect rc(10,10,100,100);
CWnd *pAvi=pParentWnd->GetDlgItem(IDC_XX_AVI);
if (pAvi)
{
pAvi->GetWindowRect(&rc);
pParentWnd->ScreenToClient(&rc);
}
UINT nIdAVI=pInfo->nIdAvi;
HANDLE hEvent=pInfo->hEvento;
delete pInfo;
pInfo=NULL;
// Create the animation control.
try
{
CAnimateCtrl cAnimCtrl;
if (!cAnimCtrl.Create(WS_CHILD|WS_VISIBLE|ACS_CENTER,
rc, pParentWnd, IDC_XX_AVI))
{
::SetEvent(hEvent);
return false;
}
// Open the AVI file.
if (!cAnimCtrl.Open(nIdAVI))
{
::SetEvent(hEvent);
return false;
}
// Pump message from the queue until the stop play message is received.
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) && (msg.message != WM_STOPCLIP))
{
switch (msg.message)
{
// Start playing from the first frame to the last,
// continuously repeating.
case WM_PLAYCLIP:
if (!cAnimCtrl.Play(0, -1, -1))
return false;
break;
}
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
cAnimCtrl.Stop();
cAnimCtrl.Close();
}
catch(...)
{
_com_error e(::GetLastError());
TRACE("\n--> Error: %s", e.ErrorMessage());
}
::SetEvent(hEvent);
return true;
}
BOOL CWorkingDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CenterWindow();
m_hEventAvi=::CreateEvent(NULL,TRUE,FALSE,"Event_AVI");
AviInfo *pInfo=new AviInfo;
pInfo->hWndParent=GetSafeHwnd();
pInfo->nIdAvi=m_nAVI;
pInfo->hEvento=m_hEventAvi;
CWinThread *pThread=AfxBeginThread(MyClipThreadProc, pInfo);
if (pThread)
{
m_hThreadAvi=pThread->m_hThread;
m_dwThreadId=pThread->m_nThreadID;
::PostThreadMessage(m_dwThreadId, WM_PLAYCLIP, 0L, 0L);
}
else
delete pInfo;
return TRUE;
}
ALMC
|
|
|
|
|
Did you use your debugger to see where it crashes exactly ?
Moreover, don't post a large segment of code like that (and also use the pre tag to format it a little bit better so that it's readable).
|
|
|
|
|
Hi,
Sorry about the formating...
The debugger is only showing some assembly, and the call stack is somewhere in the MFC...
Not much help...
ALMC
|
|
|
|
|
almc wrote: Not much help...
So start peeling away little bits of code until you are left with only what's necessary to reproduce the problem.
"Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|