![]() |
portable thread implementation problem
Heya!
Im trying to create a portable thread library, as you can see this is just a test with POSIX threads... Im confused as to why this is causing a seg fault... :
#include <iostream>hope someone can help! thanks! :D |
You need to pass a start_routine to pthread_create. Otherwise, the new thread won't know what to do.
|
hey man! thanks for the reply!
ah thats why! thanks for pointing that out :) |
heya!
i spent the whole weekend studying open source software - looking for a way to write a portable thread class. ive written a simple Thread class and well im having a problem understanding why this code is running like so: :
#include <iostream>this is the output :
Executing Test thread...i dont understand why the thread is being executed twice??? hope someone can help! thanks! :D |
Your thread is being run through the threadFunction, but you are also calling run() again in the main(). Take out the tt.run(); call in your main().
I'd recommend that you change the way your thread class works so that it doesn't autostart. Otherwise you cant create a few different threads objects and control when they start. |
heya!
thanks for the reply! ok, i already tried that, didnt work, i guess this is what you meant? ive changed it, so the thread doesnt autostart - all i have to do is call the start() method and the thread will begin executing: :
#include <iostream>i get this: :
pure virtual method calledhope you can help! thanks! :) |
The problem is that your tt variable is going out of scope before the thread has time to work. Even though you have the pthread_join in the destructor of Thread, I think by that by the time the inner destructor is called, the virtual function pointers have been removed.
You can test this by getting some keyboard input after the "start" call. The thread then works. This is probably why your first version worked (twice) the thread had a chance to start before the object went out of scope. Also note that you should initialise "thread" to 0 in the constructor, just in case somebody creates a Thread object but doesn't call start. You could add a "WaitForExit" function and call it in your derived class destructor, but that makes it a bit less convenient to use. I think because threading is run concurrently, it doesn't lend itself well to objects you can create on the stack, as they will get destroyed when they fall out of scope, even if the thread is still running. At work we use a reference counter approach, so that the object is only destroyed once the thread is finished and the calling function has indicated it is finished as well. |
Another approach you could try is to make the constructor(s) private, and have static 'factory methods' to create instances. These instances could create the objects on the heap, so you wouldn't have problems with them being destructed (yes, desctructed) prematurely. You'd still need to make sure you don't lose the reference before the thread gets stopped and the object gets released, or you'll have a memory leak, but there are ways to encapsulate much of this fucntionality as well.
|
hey guys!
thanks for the replies! oh ok, that makes sense! mmm... i guess reference counting could work, but thats a little inconvenient... or i could try adding a conditional in my start() method but again thats a hassle... do you have any links to any tutorials/open source that you reccomend? ive been googling a while and found a couple, but both work the same way (using conditionals) thanks guys! :) |
hey dont worry guys, ive fixed it!
thanks for the help! :D |
| All times are GMT -5. The time now is 2:49 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC