Asynchronous function?

U

Uli Kunkel

I need to write an asynchronous function so when I call it the program
continues executing even though the function didn't finish.

Is there a simple way to do this without any additional library's?

Thanks for any suggestions.
 
S

SG

I need to write an asynchronous function so when I call it the program
continues executing even though the function didn't finish.

Search for "Boost.Thread" (it's a library).
Is there a simple way to do this without any additional library's?

No. -- But there will be in future C++.

Cheers!
SG
 
E

Esc

I need to write an asynchronous function so when I call it the program
continues executing even though the function didn't finish.

Is there a simple way to do this without any additional library's?

Thanks for any suggestions.

I believe you need to start new thread in function then return control
back to caller.
 
B

Baron Samedi

I need to write an asynchronous function so when I call it the program
continues executing even though the function didn't finish.

Is there a simple way to do this without any additional library's?

Thanks for any suggestions.

Why not use a "callback function"? The function which you call takes
a parameter which is a function to call when it is completed. Of
course, you need threading for this. How you do it depends on your
target.

Is this a Windows program, something embedded? What's the target o/s?
 
U

Uli Kunkel

Baron said:
Why not use a "callback function"? The function which you call takes
a parameter which is a function to call when it is completed. Of
course, you need threading for this. How you do it depends on your
target.

Is this a Windows program, something embedded? What's the target o/s?


I heard about callback functions but I didn't find an easy example.
Can I call te callback function with null value for the function to return?
I just need to start a function and continue without waiting for the result.

It is a simple Windows program.
 
K

kmakaron11

Spawn then a new thread in detached state. I down know how it is done
in windows, btw.
 
I

Ivan

I need to write an asynchronous function so when I call it the program
continues executing even though the function didn't finish.

Is there a simple way to do this without any additional library's?

Thanks for any suggestions.

It is out of way to implement this in an single process or thread. You
can spawn a new thread to execute the function, after the creation,
original function will continue execution. So the basic implementation
is multi-thread. But there is no thread library in c++ standard, So
you have to chose one which is tied to operating system. I am not
familiar with window, If in linux, I will chose posix multi-thread
library.
 
J

James Kanze

It is out of way to implement this in an single process or
thread. You can spawn a new thread to execute the function,
after the creation, original function will continue execution.
So the basic implementation is multi-thread. But there is no
thread library in c++ standard, So you have to chose one which
is tied to operating system. I am not familiar with window,
If in linux, I will chose posix multi-thread library.

There are a number of multi-platform thread libraries available
for C++, so you don't have to write code tied to one platform.
(All of our low-level code works on Windows, as well as Solaris
and Linux.)
 
I

Ivan

There are a number of multi-platform thread libraries available
for C++, so you don't have to write code tied to one platform.
(All of our low-level code works on Windows, as well as Solaris
and Linux.)

Yes, agree!

Would you like to get more details about these thread libraries in C+
+?
As I know POSIX threads(also known as pthread library) can be worked
on Unix like operating system, but I don't what the compatibility
windows is.

In addition, a brief tutorial of pthread can be available at
https://computing.llnl.gov/tutorials/pthreads/.
 
J

James Kanze

Yes, agree!
Would you like to get more details about these thread
libraries in C++?

Would you? I'm familiar (more or less) with some of them.
Professionally, I currently use one that was written in house,
more than ten years ago. For new use, I would recommend
Boost---it's really low level, so you'll probably want to wrap
parts of it, and the way it handles threads themselves is a bit
wierd (and not very safe---but it can easily be wrapped), but
it's widely used, and has served as the basis of discussion for
what has been adopted by the committee. If you're coming from a
Unix background, it also has the advantage of more or less
implementing the Posix philosophy (rather than the Windows
one---although starting with Vista, I think Windows has also
moved in the direction of Posix).
 
I

Ivan

Would you?  I'm familiar (more or less) with some of them.
Professionally, I currently use one that was written in house,
more than ten years ago.  For new use, I would recommend
Boost---it's really low level, so you'll probably want to wrap
parts of it, and the way it handles threads themselves is a bit
wierd (and not very safe---but it can easily be wrapped), but
it's widely used, and has served as the basis of discussion for
what has been adopted by the committee.  If you're coming from a
Unix background, it also has the advantage of more or less
implementing the Posix philosophy (rather than the Windows
one---although starting with Vista, I think Windows has also
moved in the direction of Posix).

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Yeah, the Boost.thread is the best selection.

I am no clear about the relationship between window and POSIX, bellow
section come from
http://en.wikipedia.org/wiki/POSIX#POSIX_for_Windows

--------------------------------------------------------------------------------
Compliant via compatibility feature

The following are not officially certified as POSIX compatible, but
they conform in large part to the standards by implementing POSIX
support via some sort of compatibility feature, usually translation
libraries, or a layer atop the kernel. Without these features, they
are usually noncompliant.

* eCos – POSIX is part of standard distribution, and used by many
applications. 'external links' section below has more information.
* Plan 9 from Bell Labs APE - ANSI/POSIX Environment[9]
* Symbian OS with PIPS (PIPS Is POSIX on Symbian)
* OpenVMS (through optional POSIX package)
* Windows NT kernel when using Microsoft SFU 3.5 or SUA
o Windows 2000 Server or Professional with Service Pack 3 or
later. To be POSIX compliant, one must activate optional features of
Windows NT and Windows 2000 Server.[10]
o Windows XP Professional with Service Pack 1 or later
o Windows Server 2003
o Windows Vista
* z/OS

--------------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top