PDA

View Full Version : C++ : how to run a function and main program simultaneously?



perham
April 26th, 2010, 12:38 PM
well, the title describes it. all I want to do is to run a function while my program is running simultaneously. should I create another thread for that? and if I create another thread, can I access my data in the main program?

any idea in which ways I can do this, and which is simpler to learn?

thanks in advance

mmix
April 26th, 2010, 01:04 PM
https://computing.llnl.gov/tutorials/pthreads/

dragos2
April 26th, 2010, 01:29 PM
Use threads. But it sounds like your design is bad.

MadCow108
April 26th, 2010, 01:33 PM
have a good look at the post provided by mmix

and then "forget" it and use boost threads ;) (or any other c++ thread implementation)
boost threads are a lot easier to handle than raw pthreads:
http://www.boost.org/doc/libs/1_42_0/doc/html/thread.html
their also very similar to the threads proposed in the upcoming new standard, so no waste to learn them now.

doomguard88
April 26th, 2010, 03:55 PM
Well you can use the Qt framework to write a multitreaded programm.

Qt is a complete framework for developing gui and network capable applications
which also be compiled for every platform with no changes.

Check the link below if you are interested. :)


http://doc.qt.nokia.com/4.6/threads.html

http://qt.nokia.com/support

perham
April 26th, 2010, 04:31 PM
https://computing.llnl.gov/tutorials/pthreads/


have a good look at the post provided by mmix

and then "forget" it and use boost threads ;) (or any other c++ thread implementation)
boost threads are a lot easier to handle than raw pthreads:
http://www.boost.org/doc/libs/1_42_0/doc/html/thread.html
their also very similar to the threads proposed in the upcoming new standard, so no waste to learn them now.

thanks. I look into them.

perham
April 26th, 2010, 04:34 PM
Use threads. But it sounds like your design is bad.




Well you can use the Qt framework to write a multitreaded programm.

Qt is a complete framework for developing gui and network capable applications
which also be compiled for every platform with no changes.

Check the link below if you are interested. :)


http://doc.qt.nokia.com/4.6/threads.html

http://qt.nokia.com/support

thanks for the links. the problem is that I'm modifying a program already written using GTK, and implementing QT now is quite hard, and I have to either use the same design or rewrite huge parts of the code.

doomguard88
April 26th, 2010, 06:36 PM
Well you are right re-writing the whole thing isn't an option.
You are better off with GTK.

If you come to a dead-end check out Qt i find its documentation much better and easier to follow.

Good luck.