how to implement Mutex in C under windows

D

dharmdeep

Hi friends,
I need to know as how can I implement Mutex in C. As I
made a program in which more then one client access the same function
at a time leading to memory related error. If u can help me with sample
code its worth great help to me.
 
I

Ian Collins

Hi friends,
I need to know as how can I implement Mutex in C. As I
made a program in which more then one client access the same function
at a time leading to memory related error. If u can help me with sample
code its worth great help to me.
We don't have a u here.

Look up the threading primitives on your platform, if it is supports
threads with shared memory, it will provide some form of mutual
exclusion lock.
 
K

Keith Thompson

I need to know as how can I implement Mutex in C. As I
made a program in which more then one client access the same function
at a time leading to memory related error. If u can help me with sample
code its worth great help to me.

You can't do it in standard C, which has little or no concept of
concurrency. Try a newsgroup that deals with your system.
 
W

websnarf

I need to know as how can I implement Mutex in C. As I
made a program in which more then one client access the same function
at a time leading to memory related error. If u can help me with sample
code its worth great help to me.

This is not the newsgroup for this. Nevertheless, you can do this with
with some atomic instructions and the Sleep() function (which blocks.)
The xchg and cmpxhg8b instructions are atomic test and set
instructions, and Sleep, which blocks at set number of microseconds is
just part of the Windows API. The way I have done it in the past is to
implement a sort of Ethernet-like exponential back off. So you do the
atomic test and set, and if it fails you Sleep() for a short random
unit. And if it fails again, you double the range of the random delay
and Sleep again for that random amount of time, and keep repeating
until you hit some upper bound (which I arbitrarily chose at a tenth of
a second.) Of course the Windows API also includes its own Mutex
object that you can use as well.
 
W

Walter Roberson

This is not the newsgroup for this.

That part is right.
Nevertheless, you can do this with
with some atomic instructions and the Sleep() function (which blocks.)

There are no defined atomic instructions in C: there is only
what little can be done with sig_atomic_t
The xchg and cmpxhg8b instructions are atomic test and set
instructions,

Not part of C and not present on any architecture I program for.
and Sleep, which blocks at set number of microseconds is
just part of the Windows API. The way I have done it in the past is to
implement a sort of Ethernet-like exponential back off. So you do the
atomic test and set, and if it fails you Sleep() for a short random
unit. And if it fails again, you double the range of the random delay
and Sleep again for that random amount of time, and keep repeating
until you hit some upper bound (which I arbitrarily chose at a tenth of
a second.) Of course the Windows API also includes its own Mutex
object that you can use as well.

The original poster did not indicate a platform of implementation.
The poster's headers did indicate that (unless the client forged them)
the poster is using a Windows system to post to usenet with, but
we don't know what kind of system they are developing for.

Your response was decidedly not clear on indicating that your response
was Windows specific and not likely to work on other systems.
 
K

Kenny McCormack

That part is right.

A small sop for you guys. You should be thanking him.
There are no defined atomic instructions in C: there is only
what little can be done with sig_atomic_t

Blah, blah, blah.
Not part of C and not present on any architecture I program for.

Blah, blah, blah.
The original poster did not indicate a platform of implementation.
The poster's headers did indicate that (unless the client forged them)
the poster is using a Windows system to post to usenet with, but
we don't know what kind of system they are developing for.

Your response was decidedly not clear on indicating that your response
was Windows specific and not likely to work on other systems.

Try googling "psychic tech support". Sometimes, you just have to go
with your gut. I'm 95% sure, Paul got it right.
 
T

Tor Rustad

(e-mail address removed) skrev:
Hi friends,
I need to know as how can I implement Mutex in C. As I
made a program in which more then one client access the same function
at a time leading to memory related error.

This sound as a design problem, the trick to write thread-safe
functions, is to avoid global state... client specific state-data can
be malloc'ed or pre-alloced, if there is no global data... there is no
need to implement locks.

If your function really need to manipulate a global state, well then
you can't do the syncronization in ISO C, but need to look at e.g.
POSIX threads, which is topical in <comp.programming.threads>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top