The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Boost thread state
Discuss Boost thread state in the C Programming forum on Dev Shed. Boost thread state C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 7th, 2006, 12:12 PM
|
|
Contributing User
|
|
Join Date: Apr 2006
Posts: 80
Time spent in forums: 1 Day 6 h 59 m 43 sec
Reputation Power: 8
|
|
|
Boost thread state
Hello,
I'm sorry for posting here but I have searched google and the boost website and can not find information on this, so I was hoping someone familiar with the library could help. I would like to know if it is possible to check the state of a thread ( just if it is running/open ( don't know the correct term) or not), perhaps from another thread or main, so that if an error occurs within that thread which forces it to exit, the rest of the program can know about it.
This is because I have noticed that if I try to erase() from a vector using an iterator that does not point to any member (I tried one after the last element) the program closes, and I tried catching an exception but it doesnt seem to throw one.
So I really have two questions: First, how can I check if the iterator is valid, or through some other way avoid deleting a bad iterator? And second, how can I see if the thread has closed or not, so that I can restart the thread in the event that this or any other error forces it to close?
Thank you!
|

August 7th, 2006, 05:02 PM
|
|
Contributing User
|
|
Join Date: Apr 2006
Posts: 80
Time spent in forums: 1 Day 6 h 59 m 43 sec
Reputation Power: 8
|
|
This vector error is confusing me. Why does erase() return an iterator to the next element, why not return a bool for success? Why doesn't the function determine if the iterator is valid and not delete if it isnt? I tried the following:
Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
vector<int>::iterator i;
v.push_back(2);
v.push_back(3);
v.push_back(4);
i=v.begin();
i+=3;
cout << *i << endl;
v.erase(i);
cout << '.' << endl;
return 0;
}
Dereferencing the bad iterator prints 0 but trying to delete its element causes the program to close (I assume because the dot is never printed), why is this?
Thanks.
|

August 8th, 2006, 01:54 PM
|
 |
C++arl!
|
|
Join Date: Mar 2006
Location: Stockholm
|
|
Quote: | Originally Posted by speedy6 Hello,
I'm sorry for posting here but I have searched google and the boost website and can not find information on this, so I was hoping someone familiar with the library could help. I would like to know if it is possible to check the state of a thread ( just if it is running/open ( don't know the correct term) or not), perhaps from another thread or main, so that if an error occurs within that thread which forces it to exit, the rest of the program can know about it.
Thank you! |
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/hookreference/hookfunctions/foregroundidleproc.asp
Don't know if this is what you are looking for but it does handle thread-states. Now this is kinda complex programming and I dont know how good you are with API-hooks but this would be a cool way to do that.
//Lingon
|

August 8th, 2006, 03:13 PM
|
|
Contributing User
|
|
Join Date: Apr 2006
Posts: 80
Time spent in forums: 1 Day 6 h 59 m 43 sec
Reputation Power: 8
|
|
|
I'm sorry, I'm not familiar with hooks at all but I will try and learn what they are and how to use them now. It seems at first glance from that link where it says: "The system calls this function whenever the foreground thread is about to become idle." that the function is not called when the thread terminates abruptly?
I was hoping to use boost directly for this since I'm using boost threads already. But if this is not possible to do directly from the library I will try use your suggestion.
Thank you lingon.
|

August 8th, 2006, 04:25 PM
|
 |
C++arl!
|
|
Join Date: Mar 2006
Location: Stockholm
|
|
I belive that so called API (Application Program Interface) hooks are capable of monitoring almost any, if not all, aspects of a program (as long as it is a simple console or windows application, games and stuff like that, generally use OpenGL or DriectX wich dont tell the operative system everything they do) so it should deffinetly be worth learning, but as you proboably have understood boost threads arent quite my subject, so I dont know if the library can handle this in a much simpler way. In case you dont know, a program always sends messages of different kinds to the OS wich then executes these orders so a hook like http://msdn.microsoft.com/library/d...callwndproc.asp would deffinatly note if a thread was closed. Using API-hooks one can also moddify the messages sent from the program to the OS before they reach the OS, thus alowing us to some extent control a third party program.
Here is a link about boost threads. Hope its what you need. http://www.ddj.com/dept/cpp/184401518
|

August 8th, 2006, 06:29 PM
|
|
Contributing User
|
|
Join Date: Apr 2006
Posts: 80
Time spent in forums: 1 Day 6 h 59 m 43 sec
Reputation Power: 8
|
|
|
Thanks for your help lingon. I have read that article but it does not discuss thread states and I can not see how I can use it to find out when a thread terminates.
I'm still reading those links about hooks but I do not use the windows API at all so I am finding it difficult to understand. By your description they sound very useful in lots of cases so I will try to understand them. Thanks.
|

August 9th, 2006, 10:46 AM
|
 |
C++arl!
|
|
Join Date: Mar 2006
Location: Stockholm
|
|
np 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|