msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Semiphore critical code between VB6 threads

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Enterprise
Author Message
John Fors



Joined: 04 Oct 2007
Posts: 3

PostPosted: Thu Sep 11, 2003 1:57 pm    Post subject: Semiphore critical code between VB6 threads Reply with quote

Hi I think I need to semiphore protect some code in an object (vb6 class)
would like to get a pointer
about whats the best way to do this and get some resources to study. Thanks
in advance for you time
and help.

John

Archived from group: microsoft>public>vb>enterprise
Back to top
View user's profile Send private message
Sin



Joined: 04 Oct 2007
Posts: 25

PostPosted: Thu Sep 11, 2003 4:53 pm    Post subject: Re: Semiphore critical code between VB6 threads Reply with quote

> Hi I think I need to semiphore protect some code in an object (vb6 class)
> would like to get a pointer
> about whats the best way to do this and get some resources to study.
Thanks
> in advance for you time
> and help.


That would be a sem_A_phore John...

A semaphore is usually used to control access to something which can be
accessed by more than one thread at the same time. For example you have 10
threads, but only 3 objects to do the job... 3 use it, and the 7 others wait
for the semaphore to be released by one of the 3 threads that acquired it.

If you have ONE point which you want to limit access, a Mutex (Mutual
Exclusion) is more appropriate. Look at the following functions :

CreateMutex()
OpenMutex()
WaitForSingleObject()
ReleaseMutex()
CloseHandle()

If both threads are within the same app (that is, if you can share the
handle), an unnamed mutex will be fine. If the threads are different
applications, then a named mutex (with OpenMutex()) is the way.

If you only have one thread, then you're wasting your time, you do not need
a mutex or semaphore...

Alex.
Back to top
View user's profile Send private message
Tony Proctor



Joined: 04 Oct 2007
Posts: 101

PostPosted: Thu Sep 11, 2003 11:03 pm    Post subject: Re: Semiphore critical code between VB6 threads Reply with quote

I was just going to post his myself Alex. :Smile

Anyway, since you beat me to it, I'll just add a note about critical
sections...

Normally, multi-threaded applications use CRITICAL_SECTION objects for
mutual exclusion between the threads. However, they rely on being able to
have a shared piece of memory containing the CRITICAL_SECTION object (which
I think is about 7 long integers in size). Unfortunately, VB6's limited
multi-threading capability prevents you from sharing any data at all between
your threads. If you tried defining a CRITICAL_SECTION datum in a VB6 Module
file then each thread would end up with its own copy, and they wouldn't be
synchronising against each other at all.

It is possible to forcible share a piece of new memory between the threads,
but it's much easier to use Mutex objects as Alex suggested. They're a
little slower than critical sections because they can also synchronise
between different applications, as well as different threads in the same
application. Another advantage is that it doesn't matter which thread
accesses the mutex first - the first one usually creates it automatically
and subsequent ones merely open it. With critical sections, one thread has
to be the master and initialise the CRITICAL_SECTION object before the
others can use it.

Tony Proctor

"Sin" wrote in message@TK2MSFTNGP09.phx.gbl...
> > Hi I think I need to semiphore protect some code in an object (vb6
class)
> > would like to get a pointer
> > about whats the best way to do this and get some resources to study.
> Thanks
> > in advance for you time
> > and help.
>
>
> That would be a sem_A_phore John...
>
> A semaphore is usually used to control access to something which can be
> accessed by more than one thread at the same time. For example you have 10
> threads, but only 3 objects to do the job... 3 use it, and the 7 others
wait
> for the semaphore to be released by one of the 3 threads that acquired it.
>
> If you have ONE point which you want to limit access, a Mutex (Mutual
> Exclusion) is more appropriate. Look at the following functions :
>
> CreateMutex()
> OpenMutex()
> WaitForSingleObject()
> ReleaseMutex()
> CloseHandle()
>
> If both threads are within the same app (that is, if you can share the
> handle), an unnamed mutex will be fine. If the threads are different
> applications, then a named mutex (with OpenMutex()) is the way.
>
> If you only have one thread, then you're wasting your time, you do not
need
> a mutex or semaphore...
>
> Alex.
>
>
Back to top
View user's profile Send private message
Jens Neuhalfen



Joined: 04 Oct 2007
Posts: 55

PostPosted: Fri Sep 12, 2003 2:51 pm    Post subject: Re: Semiphore critical code between VB6 threads Reply with quote

Hi group(s)

If anyone is interested, I recently posted a vb-wrapper for semaphores
to microsoft.public.vb.general.discussion (subject "Re: Previous
instance") and have one for mutexes lying around here (somewhere).

If you are interested I will post the code

Jens

PS:
>
> It is possible to forcible share a piece of new memory between the threads,
How? This would be most interesting to me (if it is without using
VirtualAlloc and a fixed adress).

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Enterprise All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group