473,756 Members | 3,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3201
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.NO SPAM.com> wrote in message
news:D2******** *************** ***********@mic rosoft.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.p ol.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.NO SPAM.com> wrote in message
news:D2******** *************** ***********@mic rosoft.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.co m>
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
2599
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 be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this interruption occur. Will it only occur when the worker thread is...
1
1785
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, sinks events from outside sources. I realize the worker thread will be interrupted to handle an incoming event (and the flow of execution subsequently diverted to the respective event handler). My question is at what point or points could this...
20
2415
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 line 1 and Thread B handles call activity on telephone line 2. They use a common SQL datasource, but all DataSets are unique to each thread. Is there a way for thread A to occasionally communication to thread B that something has happened? ...
6
1418
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 but using it under certain circumstances still seems reasonable. For instance, I want to display a small modal dialog with a "Cancel" button which allows the user to abort a background thread. If the user clicks this button, I then want to prompt...
13
3602
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
18032
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 will almost always lead to the user opening a certain child window that's fairly resource intensive to load. Is it possible to load this form in a backgroundworker and then use the Show method and hide method as necessary? Anyone know of
0
1978
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 a LOT of synchronization overhead. After a bit of digging I concluded that this synchronization has to do with the access to a global locale inside the stream. The problem can be seen by running the small distilled benchmark code
19
2310
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
2984
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 the big changes I want to make is event-driven code (rather than the linear flow I had in Java). I have spent a week or so searching Google, talking to a couple of programming friends, and just chewing on it in my brain. I think I have an ok handle...
0
9273
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9841
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9711
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8712
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5141
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.