Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming.threads > #3354 > unrolled thread
| Started by | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| First post | 2016-05-22 19:19 +0200 |
| Last post | 2016-05-23 12:55 +0200 |
| Articles | 2 — 1 participant |
Back to article view | Back to comp.programming.threads
WaitForMultipleObjects never wakes up - Windows-bug? Bonita Montero <Bonita.Montero@gmail.com> - 2016-05-22 19:19 +0200
Re: WaitForMultipleObjects never wakes up - Windows-bug? Bonita Montero <Bonita.Montero@gmail.com> - 2016-05-23 12:55 +0200
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2016-05-22 19:19 +0200 |
| Subject | WaitForMultipleObjects never wakes up - Windows-bug? |
| Message-ID | <nhspns$15q$1@news.albasani.net> |
I've written a very efficient condvar-implementation for Win32.
Here it is: http://pastebin.com/fdjHx9ec
I'm pretty sure it's considerably more efficient than the
condvars in Win32 since Vista and Windows Server 2008.
But I had a strange phnomenon when a thread holding the condvar
locked is singalling a waiting thread and after this is sleeping
before it is releasing the lock.
I extracted the problem in a simple programm without this kind
of efficient locking through atomic variables, but just with an
event and a semaphore. Here it is:
#include <windows.h>
#include <stdio.h>
HANDLE hEvt,
hSema;
bool volatile fReleased = false;
DWORD WINAPI LockAndReleaseThread( LPVOID lpvThreadParam );
int main()
{
int const NTHREADS = 2;
HANDLE ahWait[2];
ahWait[0] = ::hEvt = CreateEvent( NULL, FALSE, TRUE, NULL );
ahWait[1] = ::hSema = CreateSemaphore( NULL, 0, 1, NULL );
for( int i = 0; i < NTHREADS; i++ )
CreateThread( NULL, 0, LockAndReleaseThread, NULL, 0, NULL );
for( ; ; )
{
WaitForMultipleObjects( 2, ahWait, TRUE, INFINITE );
printf( "main thread is holding lock and received signal\n" );
::fReleased = false;
SetEvent( hEvt );
}
return 0;
}
DWORD WINAPI LockAndReleaseThread( LPVOID lpvThreadParam )
{
for( ; ; )
{
WaitForSingleObject( hEvt, INFINITE );
printf( "spawned thread is holding lock\n" );
if( !::fReleased )
ReleaseSemaphore( ::hSema, 1, NULL ),
::fReleased = true;
Sleep( 1000 );
SetEvent( hEvt );
}
return 0;
}
The strange thing here is, that whith my Windows 10 computer,
WaitForMultipleObjects in the main-thread never awakens!
When I set NTHREADS to 1, the main-thread gets a chance, but
beyond it will freeze.
Has anyone an explanation for that?
Thanks in advance,
Bonita Montero
--
http://facebook.com/bonita.montero/
---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus
[toc] | [next] | [standalone]
| From | Bonita Montero <Bonita.Montero@gmail.com> |
|---|---|
| Date | 2016-05-23 12:55 +0200 |
| Message-ID | <nhunin$prv$1@news.albasani.net> |
| In reply to | #3354 |
I've got a solution for this problem.
In the main-thread, simply wait for the semaphore and then for the
event (in this order - or you're gonna deadlock) with WaitForSingle
Object. That's not so efficitent, but works.
int main()
{
int const NTHREADS = 2;
HANDLE ahWait[2];
::hEvt = CreateEvent( NULL, FALSE, TRUE, NULL );
::hSema = CreateSemaphore( NULL, 0, 1, NULL );
fReleased = false;
for( int i = 0; i < NTHREADS; i++ )
CreateThread( NULL, 0, LockAndReleaseThread, NULL, 0, NULL );
for( ; ; )
WaitForSingleObject( ::hSema, INFINITE ),
WaitForSingleObject( ::hEvt, INFINITE),
printf( "main thread is holding lock and received signal\n" ),
::fReleased = false,
SetEvent( ::hEvt );
return 0;
}
--
http://facebook.com/bonita.montero/
---
Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft.
https://www.avast.com/antivirus
[toc] | [prev] | [standalone]
Back to top | Article view | comp.programming.threads
csiph-web