|
Can you give me a direction where to search?
Thank you
Remon
|
|
|
|
|
Hi,
I just want to know how concurrent threads communicate to each other.
For example, there are 100 threads: thread1, thread2, thread3....thread100. AND thread1 acts as the server and all others are clients. (actually this point is not very important)
Now, thread2 sends an request to thread1 (server) for some services. (request will be sent to a global array that thread1 checks it very frequently) At this time, thread2 will suspend itself, and let thread1 to resume thread2 (ResumeThread())when thread1 thinks that it is OK to let thread2 continue. However, it is possible that thread1 will not resume thread2 very shortly due to some reasons, and it causes thread2 to wait for a long time. So, i want to add a time-out feature for thread2 here.
It is not very hard to keep track the waiting time in thread1 by using QueryPerformanceCounter(). However, how can thread1 send the time-out message telling thread2 to abort.
The main problem is TIME-OUT checking.........
Can anyone help me or have any beter solution..............(the above is my thinking...)
Thanks!!!!
|
|
|
|
|
You shouldn't use polling for inter-thread communications. You should use kernel synchronization objects (like events) and WaitForSingleObject/WaitForMultipleObjects APIs instead.
Tomasz Sowinski -- http://www.shooltz.com ** If you're going to rape, pillage and burn, be sure to do things in that order. **
|
|
|
|
|
There are two ways of stopping a thread:- Telling it to stop (somehow),
- calling
TerminateThread . The last solution is almost guaranteed to generate memory and resource leaks and leave your program in a bad shape, so I'd suggest you don't even consider it as a viable alternative. So, basically if you want to stop a thread the only way is signal this wish in the hope that the thread notices and properly stops itself. How can you signal this?- Set a boolean variable thath the thread checks frequently. Make this variable
volatile to prevent certain erroneous conditions caused by optimizations in the code by the compiler.
- Signal an event with
SetEvent . This is specially fit if the thread spends most of its time waiting for an array of objects with WaitForMultipleObjects . The approach is not as good for threads devoted to computation. - If the thread has a message pump, send it a
WM_QUIT message. Not an option in your particular scenario, I guess.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
I would suggest that the client thread post a message to thread1, passing it an event handle that thread1 will set when it thinks that the client should resume execution. Ex:
while (GetMessage(&msg, 0, 0, 0)) {
switch (msg.message) {
case WM_USER:
HANDLE hEvent = (HANDLE)msg.wParam;
::SetEvent(hEvent);
break;
}
}
The code above is part of the thread1's message pump. Now the client thead:
HANDLE hEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL);
::PostThreadMessage(dwThread1ID, WM_USER, (HANDLE)hEvent, 0);
DWORD dwWaitResult = ::WaitForSingleObject(hEvent, dwMyTimeOutInMillisecond);
if (WAIT_OBJECT_0) {
} else {
}
As you can see, thread1 receives messages from client threads, and client threads wait for thread1 to set the event, but can eventually give up waiting.
Error handling is missing for clarity.
Hope this helps
Michel
If I am wrong or said something stupid, I apologize in advance ![Wink | ;)](https://www.codeproject.com/script/Forums/Images/smiley_wink.gif)
|
|
|
|
|
I have encouraged a problem when trying to connect many clients (a thread for each client) to a single server (another thread). After the socket is received from the first client to the server, no other clients can get into the server. They are blocked!!! I've tried to use CasyncSocket. But still doesn't work. Can anyone please help me??
Thank you!![Roll eyes | :rolleyes:](https://www.codeproject.com/script/Forums/Images/smiley_rolleyes.gif)
|
|
|
|
|
Ok! I've found that different threads can get access to the server. However, another question comes up. In the same single thread (one client), how come when I try to send something to the server many times in a for loop, only the first time can access to the server (socket is received by the server), other times the access is blocked?? So, it's no longer a multithread problem. It's a single(client) thread communicate with another single thread(server).
Can someone please explain to me??
thank you!![Roll eyes | :rolleyes:](https://www.codeproject.com/script/Forums/Images/smiley_rolleyes.gif)
|
|
|
|
|
Hi,
I have a problem:
1. Thread (A) for users (1 to 100 users, so can have max 100 threads here)
2. Thread (B) for array_function
3. Global array - store all input value and work liked a queue for those inputs....(line-up)
Thread (B) keeps retreiving the item from array for input data. Thread (A) keeps sending new inputs to the array. If Thread (B) processed one input data in the queue, it will return a signal back to thread (A). Otherwise, those input data is waiting...... if the waiting time is too long, thread (A) will abort that input data.
Is it possible to implement? Can I use WaitForSingleObject(ThreadB, 5000) to do the time-out stuff?
How to return value (let say boolean flag) from thread B to thread A?
Thanks!!!!
|
|
|
|
|
Ok, here is it:
You cant write WaitForSingleObject(ThreadB, 5000), because wait functions for thread objects wait for thread termination.
Instead, you can create event object(s) with CreateEvent function prior to creating threads and give them handle (that CreateEvent returned) or name of the event(s), or write a name stringently into your code. Then if some thread alter shared data (array, etc.) it calls PulseEvent, all others WaitForSingleObject or WaitForMultipleObjects for the event(s).
Name or handle of the event(s), as well as array pointer can be determined in thread procedures by lpParameter parameter of CreateThread and CreateRemoteThread functions. This parameter is the only one of ThreadProc. Just convert it from LPVOID to LPYOUR_SHARED_MEMORY.
Hope, I explained it well
P.S.There are many other ways for interprocess signalling - mutexes, semaphores, messages, just take a look into 'Syncronization Objects' page of MSDN.
|
|
|
|
|
How to change the cursor to IDC_HAND only when the mouse is on a button ?
thanx![Rose | [Rose]](https://www.codeproject.com/script/Forums/Images/rose.gif)
|
|
|
|
|
subclass the button, then handle the WM_SETCURSOR message for the button. You can set the cursor to the hand in that message.
Or in your parent window you could simply handle the WM_SETCURSOR message, and if the cursor is over the rectangle where the button is located, you could set the hand icon.
|
|
|
|
|
Thanx![Rose | [Rose]](https://codeproject.global.ssl.fastly.net/script/Forums/Images/rose.gif)
|
|
|
|
|
I need to write a sotware to be placed on one machine and can run some of its functions on an other computer knowing the ip address
for example ( shut down an other computer using its IP address)
I need Details because I'm new to this field
thanks in advaced
MR.Byte
|
|
|
|
|
DCOM i think is what you'd be interested in reading about.
"An expert is someone who has made all the mistakes in his or her field" - Niels Bohr
|
|
|
|
|
Hi,
I have created an ActiveX control. Now i want to register this control in another machine. I copied the .ocx file to that machine and used the regsvr32 for registering the control. But i'm getting an error "LoadLibrary("control.ocx") failed. Invalid Access to memory Location."
Why is this. I have the local admin power to register the control in that machine.
Pls help
Vini
|
|
|
|
|
Could it be your .ocx is dependant on some DLLs not present in the target system? Check for dependencies from the C run-time library and MFC.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks for the reply. But i'm new to this area. So can you tell me how to check for the dependencies?
Please...
|
|
|
|
|
Sure. Go to Project->Settings->General tab. The "Microsoft Foundation Classes" combobox sets whether MFC is statically or dynamically linked ("Shared DLL"). If the latter, try switching to the former to see if your problem disappears.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
It worked!!!!!
Thanks a lot....
Vini
|
|
|
|
|
Dear friends
I am new to VC++ and I need to display a bitmap file which i stored as an resource file or u can consider it as an input file. I want to do it using Win32 API.
Will you please give me provide me any such file's complete .dsw file. It will be very helpful to me. I don't have much time, charles petzold book have so many such files but at present i don't have that book and i have to submit this project in 1-2 days. Please help me and do the needful.
Thanking you in advance.
Binay
![Cry | :((](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_cry.gif)
|
|
|
|
|
HBITMAP hBitmap;
CClientDC dc(NULL);
CString szBmpFile(_T("..."));
hBitmap = (HBITMAP)LoadImage(AfxGetInstanceHandle(),szBmpFile,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
BITMAP bm;
CDC dcMem;
::GetObject(hBitmap,sizeof(bm),&bm);
dcMem.CreateCompatibleDC(pDC);
HBITMAP pOldBitmap = (HBITMAP)(dcMem.SelectObject(hBitmap));
CRect rect
pDC->BitBlt(Rect.left,Rect.top,bm.bmWidth,bm.bmHeight,&dcMem,0,0,SRCCOPY);
|
|
|
|
|
Dear Wangyiming
Thanks a lot for ur response.But I want to get a complete source code or complete .dsw file. I always refer Charles Petzold for basic programming code. here I don't have that book. So please give me such file which is a reads one .bmp file and displays it by getting hdc. It will be a great help to me at this time. I don't have time and have so much tension. Kindly help me.
Thanking you in advance.
Binay![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
DEar Wangyiming
Thanks for ur code. I want to a complete .dsw file. I always refer Charles Petzold book fo basic programming code. So please give or send me any such file which reads a bitmap file and then display it after getting hdc.
Thanking you in advance.
Binay![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
Dear friends
I am new to VC++ and I need to display a bitmap file which i stored as an resource file or u can consider it as an input file. I want to do it using Win32 API.
Will you please give me provide me any such file's complete .dsw file. It will be very helpful to me. I don't have much time, charles petzold book have so many such files but at present i don't have that book and i have to submit this project in 1-2 days. Please help me and do the needful.
Thanking you in advance.
Binay
![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
Hi,
#define GetRandom(min, max) ((rand() % (int)(((max) + 1) - (min))) + (min))
I found this algorithm from this discussion forum. However, i found that every time it produces the same "sequence" of output. For example, GetRandom(2, 100), the sequence is 43, 55, 99, 69, 64, 84, 95, 56...............
Since i'm doing multithread, my threads get the same result when they call it.......... so...............can anyone help me???
Thanks!
|
|
|
|