ID:182161
 

I was wondering what is the equivalent of the spawn() function in C/C++. Because most of the programs I'm making now is limited because I don't know how to make functions run in the background as the spawn() function does.

Sorry for being noobish to some, but I really would like to know, hope you guys can help.
Not sure, but looks like the spawn() or spawnvp() functions are what you want. In Visual C++ spawnvp() is named _spawnvp():

http://msdn.microsoft.com/en-us/library/275khfab.aspx
I'm guessing what you'd want is threads. I'd suggest googling around for "C++ threading".
In response to Xooxer
Thanks for the quick reply, checked it out.

Ummm, one last thing, can I call a function using spawn() not just other exe files just like in DM?

[EDIT]
Thanks for the help guys! Yeah, it's pretty complicated but now I know.

This is a complicated thing called multi-threading, something not to be done by any beginner in C++ (or any language aside from a select few). Make sure you are comfortable with C++ before you start threading.

George Gough
If you're looking for a function like spawn(), you'll probably have to develop one on your own. What BYOND does is add spawn() blocks to the stack or queue or whatever BYOND uses. BYOND is not multithreaded at all!
In response to CaptFalcon33035
I'm going to make some vague guesses here. BYOND has the benefit of a VM pumping instructions. Think of it a little like a software CPU. Spawns do form what you may consider to be BYOND threads, within the VM. The VM decides what needs to be run next (this proc waiting to execute over here? This spawn block I was working on earlier, before I did something else?), switches context and runs that block, just like a CPU does. So really your C++ spawn() equivalent is logically a new thread. The catch of why spawn is easy as pie in BYOND and threads in C++ are not, is the VM takes care of locking (how do several threads share read/write access to a data-structure or file?).
In response to Stephen001
Stephen001 wrote:
Spawns do form what you may consider to be BYOND threads, within the VM. The VM decides what needs to be run next (this proc waiting to execute over here? This spawn block I was working on earlier, before I did something else?), switches context and runs that block, just like a CPU does. So really your C++ spawn() equivalent is logically a new thread. The catch of why spawn is easy as pie in BYOND and threads in C++ are not, is the VM takes care of locking (how do several threads share read/write access to a data-structure or file?).

Is that (the entire pseudo-thread thing) reproducible in C++ without the use of real threads or do we have to have specific control over a language to do that?
In response to CaptFalcon33035
You can do it, but why would you?
In response to CaptFalcon33035
Yes but that would be more difficult to implement well then just using regular threads.

George Gough
In response to Stephen001
Stephen001 wrote:
You can do it, but why would you?

I was just wondering.
With a limited knowledge of C++, I can't say much for that without researching the subject deeper, but I CAN bring up C's fork() function which allows you to "kinda-sorta" spawn another thread...using it can get pretty nasty though...
In response to Ephemerality
Fork is all about other processes, much like that spawn() function someone else linked to. IPC is a different kettle of fish entirely to thread communication. You'd need a proper design reason to want a new process, really.
In response to Stephen001
Thanks for all the info guys, much appreciated ^^