473,788 Members | 2,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Callbacks accross different threads

This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)

Sep 19 '05 #1
10 7474
Alfonso Morra wrote:
This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)


Perhaps using a mutex would help? For example, lock the mutex before calling
the callback routine. Then unlock it when you are done. This way you can
ensure that only one thread is using the callback at a time.

Just a thought,

Alvin
Sep 19 '05 #2
Alfonso Morra wrote:
This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)


Well, many GUI toolkits seem to reasonably assume just a single thread
for GUI and use queue (usually the same one as for timer events or
messages) to pass events (or callbacks) across threads.... This requires
just the queue to synchronized - MT aware, therefore I think that this
is quite a good solution simple solution.

Mirek
Sep 19 '05 #3
Alvin wrote:
Alfonso Morra wrote:

This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)

Perhaps using a mutex would help? For example, lock the mutex before calling
the callback routine. Then unlock it when you are done. This way you can
ensure that only one thread is using the callback at a time.


Well, this has the problem to defining what the mutex should serialize....

E.g., if callback is directed towards some class (like calling some
method of instance), there probably should be one mutex per class and
should be locked for any public interface method call... Not a very easy
and effective solution...

Mirek
Sep 19 '05 #4

"Alfonso Morra" <sw***********@ the-ring.com> wrote in message
news:dg******** **@nwrdmz02.dmz .ncs.ea.ibs-infra.bt.com...
This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)


What platform your are on? I've been using COM appartment concept for a
while and maybe you can either use it or reimplement it.

Ben
Sep 19 '05 #5
Alfonso Morra wrote:
This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)

You mean like Java which is OO plus threads? Just learn how to avoid
deadlock which is a problem in Java caused by naively making all method
calls synchronized without bothering to understand what locking is
used for and why and when it is or is not needed.

--
Joe Seigh

When you get lemons, you make lemonade.
When you get hardware, you make software.
Sep 19 '05 #6
On Mon, 19 Sep 2005 12:40:10 +0000 (UTC), Alfonso Morra
<sw***********@ the-ring.com> wrote:
This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and I'm
running out of both time and patience...)


I've used the following pattern successfully with a very complex multithreaded
application:
class Service
{
public:
class Listener
{
public:
virtual void Callback() = 0; // This is the callback function
};

void Do(Listener& listener);
};
The consumer of the service then creates a class that inherits from
Service::Listen er to implement the callback, and pass an instance of it to
Do(), which will then call the Callback() method.

And in some cases, the User class can inherit privately from
Service::Listen er:
class User: private Service::Listen er
{
public:
void Do(Service& service) { service.Do(*thi s); }

private:
virtual void Callback() { /* ... */ }
};
The nice thing about this pattern is that the particular resource locking and
serialization knowledge is owned by the _user_ of the service, not the service
itself. This lends itself to services that will work in both lightweight (no
synchronization needed) and heavyweight (lots of synchronization needed)
applications with no change.

If you need to synchronize the use of the service, you can either add an
Open()/Close() pair of methods to Service, or add synchronization code inside
Service::Do().

-dr
Sep 19 '05 #7


Mirek Fidler wrote:
Alfonso Morra wrote:
This may be considered as OT since the C++ Standard says not one word
about threads. Nevertheless, C++ is routinely and widely used to write
solid multithreaded code.

I wondered if anyone has any pointers/references to invoking callbacks
accross different threads? (google is not showing much promise, and
I'm running out of both time and patience...)


Well, many GUI toolkits seem to reasonably assume just a single thread
for GUI and use queue (usually the same one as for timer events or
messages) to pass events (or callbacks) across threads.... This requires
just the queue to synchronized - MT aware, therefore I think that this
is quite a good solution simple solution.

Mirek


I like, I like!. Nice simple solution. I think it may just work. Tkx !

Sep 20 '05 #8
interesting pattern - what pattern is that? I think I had seen
somethign similar in Gamma's classic book.

Sep 20 '05 #9
On 19 Sep 2005 19:33:44 -0700, "puzzlecrac ker" <ir*********@gm ail.com> wrote:
interesting pattern - what pattern is that? I think I had seen
somethign similar in Gamma's classic book.


I assume you're replying to my post (next time quote what you're replying to).

I guess the pattern is like a "reverse Bridge" pattern ;-)

Basically the strategy is for one part of the library to export an interface
which the user of the library implements.

That leaves the library free to make callbacks, and leaves the details about
what needs to be done when the callback occurs to the user.

-dr
Sep 20 '05 #10

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

Similar topics

1
2297
by: Melissa Wallis | last post by:
I have a class with 5 callbacks. Two of the callbacks work fine but the others don't. The main difference is that the callbacks that don't work are composed of a sequence of structs. I noticed a comment about this same problem on the web but no solution was noted. What makes the callbacks with the sequences so different? It seems that when one of the callbacks with a sequence is called it just hangs. I am talking to a TAO orb from...
4
2739
by: Randall Hopper | last post by:
What is the correct way to propagate exceptions from Python callbacks? When I do this: Python -> C++ -> Python Callback (example attached) an exception raised in the callback doesn't make it back across C++ to Python. It appears that PyGILState_Release() at the bottom of the callback
5
3247
by: Christopher Jastram | last post by:
I'm a self-taught programmer, so this might be a pretty dumb question. If it is, please point me in the right direction and I shall apologize profusely. I have a question regarding C++ and object members. Can anyone help? I'm writing a C++ wrapper for a fairly old programming interface to a document editing program that has no OOP whatsoever; only tons of structs. This program has different callbacks I'm supposed to implement for...
5
2247
by: Adam Clauss | last post by:
Couple questions: 1) I have an application using TCP sockets. When I make a call to BeginReceive(), is the callback I specify called in the current thread or from a new thread? 2) Similar to the first, with regards to events. When I actually call an event (with some number of delegates added to it), is each thread called sequentially in the calling thread, or is each executed asynchronously in its own?
2
3095
by: Bit byte | last post by:
I am writing an application that requires me to load a (Win32 C) libray and then register several call back functions. I am loading the libary in a worker thread and then registering functions in the worker thread in the loaded library. Two questions 1). Does loadlibrary(0 run the executable in the same thread as the calee (probably not)
9
3345
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my limited c & c++ experience. Maybe some of you can help. the problem: I need several instances of a class whose (non-static!) methods should serve as callbacks for a dll (which can' be manipulated/adapted in any
1
2204
by: geoffschmidt | last post by:
I'm trying to write an extension in C that delivers callbacks to Python. The C code starts several threads, and I'd like one of the new threads that is started to be able to deliver callbacks to Python. I thought I could do this by wrapping the callback function in PyGILState_Ensure / PyGILState_Release. When I do this, the Python code in the callback in between those two calls certainly works, but when PyGILState_Release is called, the...
4
2233
by: emma_middlebrook | last post by:
Hi Advice needed about what's the best in the following situation. In essence, I have a GUI that needs to detail time taken to do jobs that execute in their own thread. Currently, the GUI thread instantiates a class that wraps a job, handing it a callback (delegate) to call once the job has finished. On callback, always on a different thread than the GUI thread,
5
2507
by: Michael Oswald | last post by:
Hello all, I'm working on a project where I came across some situations, where the GUI library works with normal C callbacks. The code has been implemented by many people, so I came across different versions of callbacks (see the code below for 3 variants). Variant1: a normal static member function is used to redirect the callback to a member function Variant2: a static member function is declared, but at the definition the
0
10177
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
10118
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
9969
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
8995
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
7519
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
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
4074
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
3
2897
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.