|
csrss wrote: I can wait on it, so i dont have to deselect old event and select new one, so the original thread have a control over this original event
Then do you have a race condition? Which thread calls WSAEnumNetworkEvents() first? Or is only one FD_XXX event being "listened for" so WSAEnumNetworkEvents() isn't called?
csrss wrote: I can block signaling this event and signal it when i am done with my job
How do you block signalling of an event (just curious...I didn't know it was possible since I've never needed to)?
You must have intimate knowledge of the existing code to believe this is feasible
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
This is actually more an experiment then a real life solution - i am trying to create my own layer without writing and installing one. It looks like this: some event is created in some function which sends some data over a socket:
WSAEVENT hEvent = WSACreateEvent();
::WSAEventSelect(sock, hEvent, FD_WRITE | FD_READ);
WSASend(....);
And then a thread waits for event, in fact event occurs when we got some bytes waiting for us or when we are able to write some bytes. WSASend is out of the question here, because all my job is done before WSASend gets executed. The pain is WSARecv, because after this call i have some thing to do and after i am done i can return control to a function which waits for WSARecv to complete. BTW, i am hooking these calls so when WSAsomething is called control goes to my function, where i am calling WSAsomething and additionally do some work with outgoing || incoming data.
So the thing is to prevent WSAsomething completion event signaling and signal it when i am done with my part.
I know it is wrong :P and all bad things might happen and there can be a disaster crash or something and this is not what i am going to implement in final solution. I am trying and learning how to dig into system and control it in every ways possible. About blocking event, well, i can save old event handle, select new event, do my stuff, and when done, - signal old event and delete mine, for example :P Or i can go deeper into the cave and try to hook something here and there and do some crazy stuff, you know
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
csrss wrote: About blocking event, well, i can save old event handle, select new event, do my stuff, and when done, - signal old event and delete mine,
Right. Problem is, when the event is signaled, WSAEnumNetworkEvents() is usually called by the waiting/polling thread to determine which event(s) occurred. Only one thread can make that call for a given event signal on a given socket (even if you duplicate the socket) because the call resets its event status.
Good luck though
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hmmm, thanks...
Yeah, i've abandoned this idea
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
I try to read all files ( path files ) from 'Recent File List' from registry , and I failed ... I try with RegEnumValue function , but this give me string values ( File1 , File2 etc. ) not path of this files... I read RegEnumValue remarks and I see that somehow , with LPBYTE lpData parameter I could have string values , but I have no ideea how to convert LPBYTE to CString or char ... and Google didn't help me ( I didn't find a sample appropiate to my request ) ... can you help me , please ... maybe you have front with this problem ... Thank you.
|
|
|
|
|
Using the RegEnumValue() [^] function returns you the name of the value (File1 etc) and the content as a byte array. It also returns you a value item in lpType which tells you what type of data the value has. Assuming you check that this is a REG_SZ (null terminated string) then you can merely cast the pointer to the appropriate type and thence handle it as required, something like:
HKEY hKey
DWORD dwIndex = 0;
char szValueName[256];
DWORD cchValueName = 256;
DWORD dwType;
BYTE bData[4096];
DWORD cbData = 4096;
RegEnumValue(hKey, dwIndex, szValueName, &cchValueName, NULL, &dwType, bData, &cbData)
LPSTR pszValue = reinterpret_cast<LPSTR>(bData);
The best things in life are not things.
|
|
|
|
|
Thank you so much , I will try your solution and let you know what I have done .
modified on Tuesday, June 21, 2011 2:52 AM
|
|
|
|
|
Up-voted - I have no idea what sort of moron would 1-vote what is a totally reasonable response to my post.
The best things in life are not things.
|
|
|
|
|
Take away bad intention from various people, you do here best job, it' always a pleasure to talk here with so many good programmers that post here from altruism. Kindly thank you.
P.S. I didn't try your code yet, catch with other things.
|
|
|
|
|
Great , does function ! Thanks again !
|
|
|
|
|
Happy to help.
The best things in life are not things.
|
|
|
|
|
I've been interested in game development and have been using C++ for the past 3 years now, as well as OpenGL just recently. But does anyone here know of a good sound API? I've seen OpenAL, which has a basic extent of features, but I'd like something a little more flexible if possible. I've also taken a look at fmod and liked the looks of it until I saw the price tags for publishing a game with it (ranged from $500 - $9000 per platform).
Thanks for any suggestions!
|
|
|
|
|
A senior C#/C++ developer asked me this question during the interview. I thought I share it with you guys.
int x= 0;
int y= 0;
thread1
{
while (x==0) { just sit here};
printf("%d",y);
}
thread2
{
y=42;
x=1;
}
He said Assume that we know for sure Thread 1 had started before thread2:
Q1) what will printf print for answer? I said 42, that was correct.
Q2) Then he said, but suppose the answer was y=0, do you know how could that happen knowing thd1 started before thd2?
I did not get the answer correct. I did think threading, mutexes,..etc it wasn't what he was trying to get at I guess! I do know the answer, but I would like to see how you might answer.
|
|
|
|
|
Well, I think, the main reason would be that because y is not marked as volatile, the compiler might have read y (into a register) before the the while loop not knowing that the variable could be modified externally.
Without that clue, I would have probably assumed that it always works... And it probably does in many cases (platform, compiler and compiler options).
Philippe Mori
|
|
|
|
|
Well his answer was that this wouldn't happen on a single processor, but would happen on multi CPU ssytems... He said that the instructions at may get out of order as the compiler might re-arrange instructions for optimization for example, so it might load x before it does y... he said this could be avoided obviously by locks which I mentioned to him, but he said that there is also this idea of "memory fence" where you tell it by some commad "asm something..", which tells the compiler to put a fence between the instructions so the instructions don't get re-arranged...
|
|
|
|
|
This is a classic case and should be something you learn to watch for in any multithreaded application.
You have multiple variables (x, y) that must be in a consistent state (when x != 0, y must have the correct value) in order for the relationship between them to be correctly interpreted.
The only way to ensure this state is to use some form of mutual exclusion around the Setter and the Tester so that both variables are looked at as a single atomic item.
As Philippe said, it might work on some platforms and some compilers but if you are ever to take your multithreaded programming skills to other platforms then you need to learn how to avoid cases like this in the general case and use the tools in the multithreaded API to control access.
|
|
|
|
|
I am not sure if you understood my post. I did answer that I would use mutex to synchrnonize and avoid wrong results. But, his question was explain how I can get the wrong result in this case and this had nothing to do with synchronization. He wanted me to see if I understood "memory fence" which I clearly never heard of to not allow instructions to be out of order while compiler optimization. 
|
|
|
|
|
Like already pointed out, not declaring x and y as volatile could be a problem: the compiler might optimize away the test on x, i. e. pull it outside the loop, or it may just load x into a register and never bother to reload it from memory. In fact, since x is not declared as volatile, the compiler may feel obliged to make sure x is never reloaded from memory!
|
|
|
|
|
Thanks for the explanation, it does make sense.
|
|
|
|
|
I am in learning process:
here I am trying to create a custom control in mfc
file 1:
InserEdit.h
#pragma once
#include <afxwin.h>
#define INSERTWINDOWCLASS L"INSERTWINDOWCLASS"
class CInsertEdit :
public CWnd
{
DECLARE_DYNAMIC(CInsertEdit)
protected:
CEdit *cid,*UserName,*PhoneNumber,*DateOfBirth,*Nationalities;
BOOL PreCreateWindow(CREATESTRUCT& cs);
void PreSubclassWindow();
public:
CInsertEdit();
~CInsertEdit(void);
BOOL Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle);
DECLARE_MESSAGE_MAP()
private:
BOOL RegisterWindowClass();
};
InsertEdit.cpp
#include "InsertEdit.h"
BEGIN_MESSAGE_MAP( CInsertEdit, CWnd)
END_MESSAGE_MAP()
IMPLEMENT_DYNAMIC(CInsertEdit, CWnd)
CInsertEdit::CInsertEdit()
{
RegisterWindowClass();
return;
}
BOOL CInsertEdit::RegisterWindowClass()
{
WNDCLASS wndcls;
HINSTANCE hInst = AfxGetInstanceHandle();
if (!(::GetClassInfo(hInst, INSERTWINDOWCLASS, &wndcls)))
{
memset(&wndcls, 0, sizeof(WNDCLASS));
wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = ::DefWindowProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = hInst;
wndcls.hIcon = NULL;
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = INSERTWINDOWCLASS;
wndcls.cbWndExtra = NULL;
if (!AfxRegisterClass(&wndcls))
{
AfxThrowResourceException();
return FALSE;
}
}
return TRUE;
}
CInsertEdit::~CInsertEdit(void)
{
}
BOOL CInsertEdit::PreCreateWindow(CREATESTRUCT& cs)
{
return CWnd::PreCreateWindow(cs);
}
void CInsertEdit::PreSubclassWindow()
{
CWnd::PreSubclassWindow();
cid=new CEdit();
BOOL what=cid->Create(WS_VISIBLE|WS_CHILD|WS_BORDER,CRect(0,0,100,24),this,1);
}
BOOL CInsertEdit::Create(CWnd* pParentWnd, const RECT& rect, UINT nID, DWORD dwStyle)
{
return CWnd::Create(INSERTWINDOWCLASS,L"",dwStyle,rect,pParentWnd,nID);
}
The problem is when i run the program it triggers to debug break point. it is created in PreSubclassWindow class. on line 3 with cig->Create calling. can anyone point out what I am doing wrong?
modified on Saturday, June 18, 2011 4:57 AM
|
|
|
|
|
This is quite difficult to read. Please edit your message and replace the <code> tags by <pre> tags for correct code block formatting. you could also help by marking the line of code where your error occurs.
The best things in life are not things.
|
|
|
|
|
Thank you for your reply.
The error occur in PreSubclassWindow() function on line(I cant tell you exact Line Number) BOOL what=cid->Create(WS_VISIBLE|WS_CHILD|WS_BORDER,CRect(0,0,100,24),this,1);.
But here is some confusing issue. I have used PreSubClassWindow instead of onCreate Window message. And I also get some example that is created based on PreSubClassWindow. They failed at the same point. i.e. creating child window.
Later I changed it to onCreate and it solved my problems.
By the way I am using Visual Studio 2010
Thank you
|
|
|
|
|
Probably the function was called too soon like before the actual HWND was allocated and it was failing at a check done by MFC.
Philippe Mori
|
|
|
|
|
Hi, I've a modal dialog box and I've tried to set a bitmap background:
case WM_ERASEBKGND:
{
HDC memDC, hdc;
HBITMAP hBmp;
BITMAP bmp;
RECT rcc;
hdc = GetDC(hDlg);
hBmp = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BMP));
memDC = CreateCompatibleDC(hdc);
SelectObject(memDC,hBmp);
GetObject(hBmp, sizeof(bmp), &bmp);
GetClientRect(hDlg,&rcc);
SetStretchBltMode(hdc, HALFTONE);
StretchBlt(hdc,
0,0, rcc.right,rcc.bottom,
memDC,
0,0,bmp.bmWidth, bmp.bmHeight,
SRCCOPY);
DeleteDC(memDC);
DeleteObject(hBmp);
ReleaseDC(hDlg,hdc);
return (INT_PTR)TRUE;
}
this code set a background bitmap, but when I move or resize the dialog controls(edit,button,listbox,...) disappear under the bitmap...what's wrong?how can i repaint correctly the dialog?
|
|
|
|
|
Hi,
Try creating the window with the WS_CLIPCHILDREN window style. Also... you might be better off painting the bitmap in WM_PAINT and returning TRUE in your WM_ERASEBKGND handler to avoid flicker.
|
|
|
|
|