C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 7th, 2006, 12:12 PM
speedy6 speedy6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 80 speedy6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old August 7th, 2006, 05:02 PM
speedy6 speedy6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 80 speedy6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #3  
Old August 8th, 2006, 01:54 PM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 17
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

Reply With Quote
  #4  
Old August 8th, 2006, 03:13 PM
speedy6 speedy6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 80 speedy6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old August 8th, 2006, 04:25 PM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 17
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

Reply With Quote
  #6  
Old August 8th, 2006, 06:29 PM
speedy6 speedy6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 80 speedy6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old August 9th, 2006, 10:46 AM
lingon's Avatar
lingon lingon is offline
C++arl!
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Location: Stockholm
Posts: 165 lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level)lingon User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 5 h 59 m 35 sec
Reputation Power: 17
np

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Boost thread state

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap