By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

how to implement Mutex in C under windows

P: n/a
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.

Nov 8 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
dh*******@gmail.com wrote:
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.

--
Ian Collins.
Nov 8 '06 #2

P: n/a
dh*******@gmail.com writes:
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.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 8 '06 #3

P: n/a
dh*******@gmail.com wrote:
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.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

Nov 8 '06 #4

P: n/a
In article <11**********************@h48g2000cwc.googlegroups .com>,
<we******@gmail.comwrote:
>dh*******@gmail.com wrote:
> 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.
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.
--
All is vanity. -- Ecclesiastes
Nov 8 '06 #5

P: n/a
In article <ei**********@canopus.cc.umanitoba.ca>,
Walter Roberson <ro******@ibd.nrc-cnrc.gc.cawrote:
>In article <11**********************@h48g2000cwc.googlegroups .com>,
<we******@gmail.comwrote:
>>dh*******@gmail.com wrote:
>> 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.

That part is right.
A small sop for you guys. You should be thanking him.
>>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
Blah, blah, blah.
>>The xchg and cmpxhg8b instructions are atomic test and set
instructions,

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.

Nov 8 '06 #6

P: n/a
dh*******@gmail.com 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>.

--
Tor <torust AT online DOT no>
"To this day, many C programmers believe that 'strong typing' just
means pounding extra hard on the keyboard". PvdL

Nov 9 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.