473,326 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Thread synchronization

hi all,

Thx to any one that can offer me help, it will be much appreciated.

iv got a multithreaded program and need to use thread synchronization. The
synchronization does not have to work across multiple processes just the
one. I was wondering if any one new which one used the least overhead. Im
at current using mutexes but was wondering if there was something a bit
faster.

Iv looked at the Interlocked class and that seemed to do almost everything i
wanted but one thing. From looking at sample code it did not seem to go
into a wait state.

Thx for any help

Scott.
Nov 17 '05 #1
4 3141
If you need synchronization solely for the purpose of incrementing,
decrementing, comparing, and/or exchanging values then the Interlocked
routines are probably what you want. I'm not sure what you mean by them "not
going into a wait state" -- they guarantee atomic operation so if two threads
try to perform an interlocked operation on the same variable concurrently,
one of them must block until the other finishes its interlocked operation!

Otherwise the lock(object o) {...} construct (based on the Monitor class)
seems pretty good. Just out of curiosity, what do you mean by "something a
bit faster" than mutexes? Have you determined that the Mutex class is a
bottleneck in your application? What latency are you seeing between one
thread releasing a Mutex and a blocked thread coming out its wait on the
Mutex? What is the cost of calling WaitOne on an unowned Mutex?

I guess I would be a little surprised if the Mutex operations were really so
heavyweight that you would notice.

-- Tom

"scott" wrote:
iv got a multithreaded program and need to use thread synchronization. The
synchronization does not have to work across multiple processes just the
one. I was wondering if any one new which one used the least overhead. Im
at current using mutexes but was wondering if there was something a bit
faster.

Iv looked at the Interlocked class and that seemed to do almost everything i
wanted but one thing. From looking at sample code it did not seem to go
into a wait state.


Nov 17 '05 #2
Thx for the reply.

sounds like mutexes might be the best ones to use then. My only worry was
that i have one thread that goes through a continues loop of up to may be
3000 or more (sleeping for 100 every time the loop is complete then starting
the loop again). Within that loop i need to use synchronization about 20
times. I just wanted to use the fastest possible way of synchronization
between 2 threads.

"Thomas W. Brown" <th************@countrywide.NOSPAM.com> wrote in message
news:D2**********************************@microsof t.com...
If you need synchronization solely for the purpose of incrementing,
decrementing, comparing, and/or exchanging values then the Interlocked
routines are probably what you want. I'm not sure what you mean by them "not going into a wait state" -- they guarantee atomic operation so if two threads try to perform an interlocked operation on the same variable concurrently,
one of them must block until the other finishes its interlocked operation!

Otherwise the lock(object o) {...} construct (based on the Monitor class)
seems pretty good. Just out of curiosity, what do you mean by "something a bit faster" than mutexes? Have you determined that the Mutex class is a
bottleneck in your application? What latency are you seeing between one
thread releasing a Mutex and a blocked thread coming out its wait on the
Mutex? What is the cost of calling WaitOne on an unowned Mutex?

I guess I would be a little surprised if the Mutex operations were really so heavyweight that you would notice.

-- Tom

"scott" wrote:
iv got a multithreaded program and need to use thread synchronization. The synchronization does not have to work across multiple processes just the
one. I was wondering if any one new which one used the least overhead. Im at current using mutexes but was wondering if there was something a bit
faster.

Iv looked at the Interlocked class and that seemed to do almost everything i wanted but one thing. From looking at sample code it did not seem to go
into a wait state.

Nov 17 '05 #3
Scott:

If you are worried about the weight, you might seriously consider using the
Monitor class instead of a Mutex. The Monitor is the same object that is
used intrinsically with a Lock() statement.

The Monitor is a light-weight synchronization object (lighter than a Mutex).
It is only visible in-process, but I believe that you stated that you do not
need for it to work cross-process anyway.

"scott" <sc***********@hotmail.com> wrote in message
news:d8**********@newsg3.svr.pol.co.uk...
Thx for the reply.

sounds like mutexes might be the best ones to use then. My only worry was
that i have one thread that goes through a continues loop of up to may be
3000 or more (sleeping for 100 every time the loop is complete then
starting
the loop again). Within that loop i need to use synchronization about 20
times. I just wanted to use the fastest possible way of synchronization
between 2 threads.

"Thomas W. Brown" <th************@countrywide.NOSPAM.com> wrote in message
news:D2**********************************@microsof t.com...
If you need synchronization solely for the purpose of incrementing,
decrementing, comparing, and/or exchanging values then the Interlocked
routines are probably what you want. I'm not sure what you mean by them

"not
going into a wait state" -- they guarantee atomic operation so if two

threads
try to perform an interlocked operation on the same variable
concurrently,
one of them must block until the other finishes its interlocked
operation!

Otherwise the lock(object o) {...} construct (based on the Monitor class)
seems pretty good. Just out of curiosity, what do you mean by "something

a
bit faster" than mutexes? Have you determined that the Mutex class is a
bottleneck in your application? What latency are you seeing between one
thread releasing a Mutex and a blocked thread coming out its wait on the
Mutex? What is the cost of calling WaitOne on an unowned Mutex?

I guess I would be a little surprised if the Mutex operations were really

so
heavyweight that you would notice.

-- Tom

"scott" wrote:
> iv got a multithreaded program and need to use thread synchronization. The > synchronization does not have to work across multiple processes just
> the
> one. I was wondering if any one new which one used the least overhead. Im > at current using mutexes but was wondering if there was something a bit
> faster.
>
> Iv looked at the Interlocked class and that seemed to do almost everything i > wanted but one thing. From looking at sample code it did not seem to
> go
> into a wait state.


Nov 17 '05 #4
scott <sc***********@hotmail.com> wrote:
iv got a multithreaded program and need to use thread synchronization. The
synchronization does not have to work across multiple processes just the
one. I was wondering if any one new which one used the least overhead. Im
at current using mutexes but was wondering if there was something a bit
faster.

Iv looked at the Interlocked class and that seemed to do almost everything i
wanted but one thing. From looking at sample code it did not seem to go
into a wait state.


Monitors are almost certainly the way to go. I suggest you read
http://www.pobox.com/~skeet/csharp/threads

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bill Davidson | last post by:
Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things, sinks events from outside sources. I realize the worker thread will...
1
by: Bill Davidson | last post by:
(RESEND: I added a little more code to the sample for clarity) Hello All: I've got a question about synchronization requiremements in a C# worker thread procedure that, among other things,...
20
by: Bob Day | last post by:
Using VS 2003, VB, MSDE... There are two threads, A & B, that continously run and are started by Sub Main. They instantiationsl of identical code. Thread A handles call activity on telephone...
6
by: Robert Speck | last post by:
Hi there, Can anyone shed anymore light on why "Thread.Suspend()" has been deprecated by MSFT beyond what MSDN says about it. I'm not sure if I quite appreciate the various pitfalls they discuss...
13
by: arun.darra | last post by:
Are the following thread safe: 1. Assuming Object is any simple object Object* fn() { Object *p = new Object(); return p; } 2. is return by value thread safe?
9
by: RvGrah | last post by:
I'm completely new to using background threading, though I have downloaded and run through several samples and understood how they worked. My question is: I have an app whose primary form...
0
by: sundman.anders | last post by:
Hi all! I have a question about thread synchronization and c++ streams (iostreams, stringstreams, etc). When optimizing a program for a multicore processor I found that stringstream was causing...
19
by: Hapa | last post by:
Does only reading (never writing) of a variable need thread synchronisation? Thanks for help? PS. Anybody knows a Visual C++ news group?
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.