473,378 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

_beginthread -- how to terminate a thread from out side of it.

As i konw, the only way to to terminate a thread started by
_beginthread() is to user _endthread() (or return) inside the thread.
now i started a thread(TodoThread), and created a listenning socket
inside the thread.
I want to destroy the socket and kill the thread at anytime if there is
no client connect to it. How?
void TodoThread(PVOID pvoid){
//creat a tcp socket
//then listen
accept();
//.....
}

Aug 24 '05 #1
9 13016
> now i started a thread(TodoThread), and created a listenning socket
inside the thread.
I want to destroy the socket and kill the thread at anytime if there is
no client connect to it. How?


What *exactly* is your *C++* question??

Srini

Aug 24 '05 #2
the simple question is :how to terminate a thread from out side of it.

sorry, i don't quite care about what is c and what is c++.

Aug 24 '05 #3
> the simple question is :how to terminate a thread from out side of it.

sorry, i don't quite care about what is c and what is c++.


C++ has no *language* support for threads. So, this is'nt the right
newsgroup to ask your query. Try finding a newsgroup of that thread
library vendor.

Srini

Aug 24 '05 #4
well, thanks.

Aug 24 '05 #5

I personally feel you can use pthreadcancel() or it equivalent to
cancel your threadfrom another thread.

Thank You,
Gevadas A. Akkara

Aug 24 '05 #6

ge*****@gmail.com wrote:
I personally feel you can use pthreadcancel() or it equivalent to
cancel your threadfrom another thread.

Thank You,
Gevadas A. Akkara


Your personal feelings cannot be understood without a context. And if
it is not
related to c++, don't discuss it here.

Krishanu

Aug 24 '05 #7

This is not topical for c.l.c++ etc etc.

li********@hotmail.com wrote:
As i konw, the only way to to terminate a thread started by
_beginthread() is to user _endthread() (or return) inside the thread.
now i started a thread(TodoThread), and created a listenning socket
inside the thread.
I want to destroy the socket and kill the thread at anytime if there is
no client connect to it. How?
void TodoThread(PVOID pvoid){
//creat a tcp socket
//then listen
accept();
//.....
}


Try comp.threads.

However, it is my opinion that you should provide a way to "wake up" the
thread. You can do this by "waking" up the thread.
void TodoThread(PVOID pvoid){
//creat a tcp socket
//then listen
int listenfd = listen(....);

if ( int N = select( n, ...., time_yadda ) )
{
if ( isset listenfd... )
connectionfd = accept( listenfd );
}
}

or use events on windows. Basically, you can't just call accept. I
think you really want to set the file descriptor to NDELAY which means
that accept will return ...

Anyhow, look at some sources that do this.

Aug 24 '05 #8
<li********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
: As i konw, the only way to to terminate a thread started by
: _beginthread() is to user _endthread() (or return) inside the thread.
:
: now i started a thread(TodoThread), and created a listenning socket
: inside the thread.
: I want to destroy the socket and kill the thread at anytime if there is
: no client connect to it. How?

You cannot safely and cleanly terminate a thread from the outside.
As C++ code, your thread's program will most likely be allocating
resources (e.g. heap-allocated memory from std::string, etc).
While some API's allow you to terminate a thread (e.g. with
cancelthread or TerminateThread), they cannot ensure that these
resources are freed.
[some platforms may allow you to throw an exception into the
context of another thread, but that's also messy and hard
if at all possible to get right]

So what should you do instead?
- Use a child process instead of a thread: a modern OS
allows you to kill/terminate a process while ensuring
that its resources are properly released.
- Send some kind of signal to the thread asking it to
exit (e.g. if the thread is waiting on a semaphore,
set an exit request flag and signal that semaphore).
You then need to wait for the thread to exit cleanly.
Cheers,
Ivan

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Aug 24 '05 #9
sorry, I didn't know _beginthread is produced by any vendor. and I am
new to newsgroup.

thanks everyone, thanks Ivan, i will try process.

Aug 24 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: liangbowen | last post by:
As i konw, the only way to to terminate a thread started by _beginthread() is to user _endthread() (or return) inside the thread. now i started a thread(TodoThread), and created a listenning...
1
by: Dim | last post by:
Hi i am trying to create a Thread using _beginthread that will access data in a class but i keep getting a C2664: '_beginthread' : cannot convert parameter 1 from 'void (DBLR<T>&)' to 'void (__cdecl...
2
by: Altman | last post by:
I have a class called cScale, I have made an array of scale objects and I need to run a method of these objects as a new thread. This is my line of code and I can't get it to run. I am still a...
5
by: JHoletzeck | last post by:
Is it possible to set the stack size for the thread created by BackgroundWorker like in the API call _beginthread? If not is the stack size a default or the one I set for the whole application?...
2
by: Abubakar | last post by:
Hi, Lets say I have a method called "listen_proc" inside "class1". There is another method called "start" in the same class that has to start the "listen_proc" inside a new thread. I am using...
3
by: Mr Dyl | last post by:
I'm using Boost to run a couple of threads, one of which reads commands via std::cin and another receives them through a socket. It's entirely possible that during the life of the app, all...
0
by: Sir Spamallot | last post by:
Hi there, Previously when handling the termination of threads I had just called the abort method and caught it in a try catch block in the thread callback. After recently learning that this was...
1
by: masaniparesh | last post by:
Hi, In my C# program i am termination thread by thread.Abort when times out occur. But i figured out that even after thread.Abort operation the thread is being alive for the random time. I have...
0
by: balach | last post by:
hi all, i am using this code to initialize and assign it to a process, i am calling a thread on every 10 Seconds to fire vbscript file, this activity is performed on "timer1_Tick( )" event and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.