473,395 Members | 1,978 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,395 software developers and data experts.

Looking for an Efficient Thread Synchronization?

In an application manipulating streaming video and 3D stuff I want to
implement mutithreading to ensure a decent UI response time.

Since my application is very speed critical I want to use the most efficient
synchronization of the shared data objects. What would that be in this case?
I think there's a very simple answer to this, but I am somewhat confused by
the different methods available.

The data I need to transfer between threads share similarities:
- The data only goes one way (streaming); One thread is writing, another
thread is reading.
Only these two threads have access to the object; pretty simple.
- The data transfered per cycle are images and therefore relatively large
chuncks, approximately 1 mb.
- There are up to 30 read/write operations per shared object per second.

Any answer is appreciated (also simple ones)
Looking forward, Teis


Nov 16 '05 #1
10 1943
Are you using a queue or must each write be followed by a read before the next write can happen?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

In an application manipulating streaming video and 3D stuff I want to
implement mutithreading to ensure a decent UI response time.

Since my application is very speed critical I want to use the most efficient
synchronization of the shared data objects. What would that be in this case?
I think there's a very simple answer to this, but I am somewhat confused by
the different methods available.

The data I need to transfer between threads share similarities:
- The data only goes one way (streaming); One thread is writing, another
thread is reading.
Only these two threads have access to the object; pretty simple.
- The data transfered per cycle are images and therefore relatively large
chuncks, approximately 1 mb.
- There are up to 30 read/write operations per shared object per second.

Any answer is appreciated (also simple ones)
Looking forward, Teis

Nov 16 '05 #2
There are two fundamental approaches to thread synchronisation. The first
and most common is lock-and-block. The other is message-based. Message-based
is faster but requires more talent.

Set up events on both caller and callee. That way you don't have to wait for
the asynch call to return, the callee can use an event to notify completion
(also asynchronously). Define subclasses of EventArgs to carry your data and
use them for all your parameter passing.

Don't explicitly fire events to call these methods, it's too slow. Call them
directly and have each one start like this so you don't incur the overhead
of asynch marshalling unless you actually need it. Note: functions are
intrinsically blocking. In this case you should use BeginInvoke() and
EndInvoke() to call asynch yet wait for the response. Not as free-wheeling
but still thread-safe.

Or rewrite your functions to return their values in EventArgs via asynch
callback. That's what I do. But you'd better be good at programming - you
can't depend on things happening in a fixed order.

delegate MyMethodDelegate(...);
event MyMethodDelegate MyMethodEvent(...);
void MyMethod(...) {
if (InvokeRequired) {
MyMethodEvent(...);
return;
}
//actual processing
...
}
Nov 16 '05 #3
Thanks you for ansering my post,

there are two different behaveiours
- In some parts, such as reading the video stream, I need to process every
single frame data. A queue sounds suitable for this task.

- In other pards, such as rendering to the UI, I just need the latest data.
The UI has a lot to do and might ask(!) for the data less frequent than
offered by the stream and should then just have the last frame. If it asks
more frequent than offered, it should be told to reuse the last frame.

Regards, Teis
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:OR****************@TK2MSFTNGP11.phx.gbl...
Are you using a queue or must each write be followed by a read before the next write can happen?
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

In an application manipulating streaming video and 3D stuff I want to
implement mutithreading to ensure a decent UI response time.

Since my application is very speed critical I want to use the most efficient synchronization of the shared data objects. What would that be in this case? I think there's a very simple answer to this, but I am somewhat confused by the different methods available.

The data I need to transfer between threads share similarities:
- The data only goes one way (streaming); One thread is writing, another
thread is reading.
Only these two threads have access to the object; pretty simple.
- The data transfered per cycle are images and therefore relatively large
chuncks, approximately 1 mb.
- There are up to 30 read/write operations per shared object per second.

Any answer is appreciated (also simple ones)
Looking forward, Teis

Nov 16 '05 #4
Thank you very much for your detailed answer! Very instructive reading.
Or rewrite your functions to return their values in EventArgs via asynch
callback. That's what I do. But you'd better be good at programming.... - Dang!
Note: functions are intrinsically blocking. Do you mean that I can implement lock-and-blocking by encapsulating access
to a specific variable in a single method to ensure that the variable is
only accessed once at a time?... Will this approach exclude me from the
community in all future?
Otherwise can you provide me with standard way to implement simple
lock-and-block?
Don't explicitly fire events to call these methods, it's too slow. Call them directly and have each one start like this so you don't incur the overhead
of asynch marshalling unless you actually need it. Do you have a reference to a code sample for using technique? (Sorry... I'm
still new to threads and will most likely screw up anything, being on my
own).
Thanks yet again, Teis

"Peter Wone" <pe****@wamoz.com> wrote in message
news:OL****************@TK2MSFTNGP14.phx.gbl... There are two fundamental approaches to thread synchronisation. The first
and most common is lock-and-block. The other is message-based. Message-based is faster but requires more talent.

Set up events on both caller and callee. That way you don't have to wait for the asynch call to return, the callee can use an event to notify completion (also asynchronously). Define subclasses of EventArgs to carry your data and use them for all your parameter passing.

Don't explicitly fire events to call these methods, it's too slow. Call them directly and have each one start like this so you don't incur the overhead
of asynch marshalling unless you actually need it. Note: functions are
intrinsically blocking. In this case you should use BeginInvoke() and
EndInvoke() to call asynch yet wait for the response. Not as free-wheeling
but still thread-safe.

Or rewrite your functions to return their values in EventArgs via asynch
callback. That's what I do. But you'd better be good at programming - you
can't depend on things happening in a fixed order.

delegate MyMethodDelegate(...);
event MyMethodDelegate MyMethodEvent(...);
void MyMethod(...) {
if (InvokeRequired) {
MyMethodEvent(...);
return;
}
//actual processing
...
}

Nov 16 '05 #5
Teis Draiby <te*************@draiby.com> wrote:
In an application manipulating streaming video and 3D stuff I want to
implement mutithreading to ensure a decent UI response time.
<snip>
- There are up to 30 read/write operations per shared object per
second.


This is the important bit, I think. If you only need synchronization
about 30 times per second, you don't need to be as efficient as
possible. It's much easier to write good threading code if you don't
require it to be absolutely as fast as possible.

The code which probably *does* need to be fast is the data transfer
part - that's a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi...
Then, maybe a blocking situation is that unlikely to occour frequently that
there is no remarkable overhead just using a simple lock-and-block.

regards, Teis


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Teis Draiby <te*************@draiby.com> wrote:
In an application manipulating streaming video and 3D stuff I want to
implement mutithreading to ensure a decent UI response time.


<snip>
- There are up to 30 read/write operations per shared object per
second.


This is the important bit, I think. If you only need synchronization
about 30 times per second, you don't need to be as efficient as
possible. It's much easier to write good threading code if you don't
require it to be absolutely as fast as possible.

The code which probably *does* need to be fast is the data transfer
part - that's a different matter.

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

Nov 16 '05 #7
Hi...
Then, maybe a blocking situation is that unlikely to occour frequently that
there is no remarkable overhead just using a simple lock-and-block.

regards, Teis


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Teis Draiby <te*************@draiby.com> wrote:
In an application manipulating streaming video and 3D stuff I want to
implement mutithreading to ensure a decent UI response time.


<snip>
- There are up to 30 read/write operations per shared object per
second.


This is the important bit, I think. If you only need synchronization
about 30 times per second, you don't need to be as efficient as
possible. It's much easier to write good threading code if you don't
require it to be absolutely as fast as possible.

The code which probably *does* need to be fast is the data transfer
part - that's a different matter.

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

Nov 16 '05 #8
Teis,

When I understand you well, can it in my opinion only be efficient what you
try to do is when your deliverer gives only a certain bandwidth for one
stream.

It is in my opinion not efficient for a diskstream, than it is probably more
busy handling the parts you need to read than the actual stream, which reads
the most efficient when there are no other threads who interrupt every time
the reading and that the disk needs to be positioned again.

Just my thought,

Cor
Nov 16 '05 #9
Hi Cor,
The stream is either a live camera stream or from footage already loaded
into memory. I'm not sure about what you're suggesting, but it seems that it
only applies if I am streaming from disk. Is that true?

,teis

"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2*****************@TK2MSFTNGP11.phx.gbl...
Teis,

When I understand you well, can it in my opinion only be efficient what you try to do is when your deliverer gives only a certain bandwidth for one
stream.

It is in my opinion not efficient for a diskstream, than it is probably more busy handling the parts you need to read than the actual stream, which reads the most efficient when there are no other threads who interrupt every time the reading and that the disk needs to be positioned again.

Just my thought,

Cor

Nov 16 '05 #10
Teis,

What I wrote was mainly as it comes from disk yes, however that camera goes
in my opinion as well over one port and I see no interupts directly in that
on the computer side where multithreading will help. My idea is that the
camera is the slowest part. For memory you have it in memory so what is it
that part that you want to improve streaming there. Even when you have a
multiprocessor or hypertreading I think that the synchronization cost you
much more than the streaming.

I am a little bit confused why you want to use threading in this and what
benefit you expect, not only for you however as well for myself.

Cor
Nov 16 '05 #11

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

Similar topics

3
by: Chris Tanger | last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
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?
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?
82
by: Bill David | last post by:
SUBJECT: How to make this program more efficient? In my program, a thread will check update from server periodically and generate a stl::map for other part of this program to read data from....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
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...
0
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,...

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.