|
Thank you! I've actually checked that out already.
And, I've found an even simpler program about that.
|
|
|
|
|
Hi,
I just wonder if my program is really protected by CriticalSection:
My program uses multithreading......and require to access many array. Since I want to prevent same time access to an array item by several threads, I centralize all operation that modify the content of array in one function. Here is the structure of this function:
int function(....)
{
CCriticalSection cs;
cs.Lock();
//code for modifying array here....
cs.Unlock();
}
However, my thread function has some codes to "read" the array content in their own thread function.
My question is: Is it OK???
I have run my program. Sometimes OK, sometimes has error (especially i choose to run so many threads...)
Any suggestion to help me debug???
|
|
|
|
|
ChiYung wrote:
Is it OK???
NO
CCriticalSection cs; <--- this is now a temp variable
Make it a member of your class or make it global
Nish
I am looking for a free racing game. Old style racing game without any complicated 3D stuff. Around 4 MB download. If anyone has such a game, please inform me.
|
|
|
|
|
Nish [BusterBoy] wrote:
Make it a member of your class or make it global
I tried to declare it in StdAfx.h and .cpp:
In StdAfx.h:
extern CCriticalSection cs;
In StdAfx.cpp
CCriticalSection cs;
But i have this error message:
"error C2146: syntax error : missing ';' before identifier 'cs'
fatal error C1004: unexpected end of file found"
What happened??
I tried to put it in ....Dlg.h...........same error.........
Can you help me!!!????
Thanks!!!!
|
|
|
|
|
Dont put it in stdafx.h
Cause when you put it on top, the compiler hasn't encountered the included header files yet
Make it a member variable of the class where you are using the critical section
Nish
I am looking for a free racing game. Old style racing game without any complicated 3D stuff. Around 4 MB download. If anyone has such a game, please inform me.
|
|
|
|
|
No, because you're creating the critical section on the stack. Each thread has its own stack, so when two threads enter the function they use different critical sections.
You need to have one critical section and then access it in all threads.
--Mike--
It's hammer time!
My really out-of-date homepage
Sonork - 100.10414 AcidHelm
Big fan of Alyson Hannigan and Jamie Salé.
|
|
|
|
|
Hi,
Thanks for all your replies.......
But I'm not quite understand........can you give me some example codes???
Thanks
|
|
|
|
|
Tell you what, since you seem to have trouble making it a member of your class, for whatever reasons do this then :-
Make it a member of the CWinApp derived class.
Now use AfxGetApp(), cast the pointer returned to your CWinApp derived class and access the critical section variable.
Nish
I am looking for a free racing game. Old style racing game without any complicated 3D stuff. Around 4 MB download. If anyone has such a game, please inform me.
|
|
|
|
|
Nish [BusterBoy] wrote:
Make it a member of the CWinApp derived class.
Now use AfxGetApp(), cast the pointer returned to your CWinApp derived class and access the critical section variable.
I'm not quite understand how to make it a member of the CWinApp, or even make it global. Can you give me some examples??
LOTS OF THANKS!!!!!!!!!
|
|
|
|
|
Dear Friend
I don't know whether this is going to solve your problem , This is how to make a variable global. double click the "theApp" Global variable in your project.then you can see the declaration
eg:
CMyApp theApp;
//Put your variable as follows
CCriticalSection cs;
//Now this is global
Then go to the implimentation file of the class where you want to use this varialble,and put is as follows
eg:
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//Global Variable, Now use this in your function
extern CCriticalSection cs;
// CMyApp
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()
Gaurika Wijeratne
|
|
|
|
|
ChiYung wrote:
CCriticalSection cs;
If you just want to protect what is happening inside this function then simple change it to:
static CCriticalSection cs;
As others have said it can't be on the stack under any circumstances.
Neville Franks, Author of ED for Windows. www.getsoft.com
|
|
|
|
|
I use the following code:
/////////////////////////////////////
this->GetDC()->SetBkColor( RGB(0,0,255) );
this->SendMessage( WM_PAINT, 0, 0 );
//////////////////////////////////////
But no efforts, what's the matter?
I'm amumu, and you?
|
|
|
|
|
|
hi,
Take a look the structure of my array first:
struct WAITQUEUE_STRUCT
{
CWinThread* pThread;
int LockType;
LARGE_INTEGER start_time;
};
struct LOCK_TABLE
{
CWinThread* CurrentThread;
int CurrentLock;
std::vector<WAITQUEUE_STRUCT> WaitQueue;
};
std::vector<LOCK_TABLE> LockTable;
In my program:
std::vector <WAITQUEUE_STRUCT>::iterator Iter;
for (int i=0; i<NumOfDBObject; i++)
{
Iter = LockTable[i].WaitQueue.begin();
if (*Iter.pThread != NULL) <- one of the error here
{ .......
.......
.......
i got the error message :
"error C2228: left of '.pThread' must have class/struct/union type"
What happened??????
|
|
|
|
|
ChiYung wrote:
for (int i=0; i{
Iter = LockTable[i].WaitQueue.begin();
if (*Iter.pThread != NULL) <- one of the error here
{ .......
(*iter).pThread
or
iter->pThread
|
|
|
|
|
DEar Friends
I always refer Charles Petzold book fo basic programming code. So I want to get a complete .dsw file. Kindly give/send me any such zip file which reads a bitmap file and then display it after getting hdc. I am working on Win32API only.
Thanking you in advance.
Binay
|
|
|
|
|
after you use LoadImage to load the bitmap, use this code in your window procedure to draw it:
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
RECT rt;
GetClientRect(hWnd, &rt);
HDC DC = CreateCompatibleDC(hdc);
HBITMAP oldBitmap = (HBITMAP)SelectObject(DC, hBitmap);
BitBlt(hdc, 0, 0, rt.right, rt.bottom, DC, 0, 0, SRCCOPY);
SelectObject(DC, oldBitmap);
DeleteDC(DC);
EndPaint(hWnd, &ps);
}
HTH
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
If I did that, I would be doing your homework for you. It should be very easy to figure out with the code I have given you. If you have any more specific questions, ask the in this forum, there are lots of people who will help you.
---
CPUA 0x5041
Sonork 100.11743 Chicken Little
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
Hi,
I would like to terminate the FOR loop at the middle of the loop. E.g.
for (i=0; i<100; i++)
{
if (i == 50)
break;
}
Is it use break???? Or.........
Thanks!!!!
|
|
|
|
|
Yes.
------------------------
Derek Waters
derek@lj-oz.com
|
|
|
|
|
Hi,
Is there a function to set a timer?? In my case, I want to keep track of the time that a thread is being suspended, so that it would be suspended forever.
Or, can you give me any suggestion to do that??
Thank you!
|
|
|
|
|
You can use SetTimer and then in the handler do your custom time tracking.
Nish
I am looking for a free racing game. Old style racing game without any complicated 3D stuff. Around 4 MB download. If anyone has such a game, please inform me.
|
|
|
|
|
Thank you so much for your reply..!
But, I still have problem with SetTimer(). I always got error messages like "function does not take 1 (or 2) parameters". Can you please give me an example of using SetTimer?? For example: what I can do if I want to count in 5 sec??
Thank you !
|
|
|
|
|
SetTimer(1000,5000,NULL);
1000 is the ID of this particular timer
5000 means 5 seconds
NULL means the WM_TIMER handler of the CWnd object gets called
Nish
I am looking for a free racing game. Old style racing game without any complicated 3D stuff. Around 4 MB download. If anyone has such a game, please inform me.
|
|
|
|
|
Thank you very much!
|
|
|
|