473,804 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

_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(TodoThre ad), 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(PVOI D pvoid){
//creat a tcp socket
//then listen
accept();
//.....
}

Aug 24 '05 #1
9 13064
> now i started a thread(TodoThre ad), 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.c om 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********@hotm ail.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(TodoThre ad), 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(PVOI D 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(PVOI D 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********@hot mail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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(TodoThre ad), 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
562
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 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?
1
1658
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 *)(void *)' with . Anyone who can help, please do so, its driving me crazy. I know its most probably a type casting problem but i don't know what to do Thank /////////////////////////////////////////////////////////////code sampl void qtest1(...
2
2904
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 little of a newbie to C++. Scales is an array of scale objects and weighup is my method. Scales.thread = _beginthread( Scales.WeighUp, 0, NULL);
5
2230
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? (What I need is a worker thread with a rather large stack in a Windows Forms app complied with /clr:pure) Cheers Jürgen
2
3884
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 CreateThread but I cannot manage to pass the right function pointer, the compiler keeps on giving error that it cant convert my given function to LPTHREAD_START_ROUTINE while I try to cast it. In a simple c++ non-class file I can easily do it but this...
3
5979
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 commands will be sent through the socket and the thread watching cin will never "see any action". So the problem is, how do I terminate this thread that waits for keyboard input? Unfortunately, Boost::thread doesn't yet allow me to force the thread...
0
1203
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 a bad way of handling thread termination I am working on terminating threads by using ManualResetEvent's. I have come up with the following code which appears to work very well (at end of message). I would like to know if anyone knows of any...
1
1275
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 given the code snippet for this below. private void Delay() { //Thread waiting for the output stream of the process DateTime expireTime = DateTime.Now.AddSeconds(30); Thread outputThread = new Thread(new ThreadStart(ReadOutput)); ...
0
1204
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 give me required information. some times i face problem that the second thread associated with "Dummy.vbs" script file, do not reach at specified remote machine, and give error message after 45 seconds to 1 minute, i want that to terminate its...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.