Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » how to stop a thread that is waiting, e.g., listen()
icon11.gif  how to stop a thread that is waiting, e.g., listen() [message #11802] Wed, 26 September 2007 11:40 Go to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
i know how to do this neither in GNU C++ nor in U++.
one thread create the other. the second thread binds a socket and listen, waiting for incoming connections, and obviously it is blocked. how does the first thread stop the second? Rolling Eyes
thank you.
Re: how to stop a thread that is waiting, e.g., listen() [message #11803 is a reply to message #11802] Wed, 26 September 2007 13:05 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
You could probably do this with a signal. Normally, threads allow you to have multiple signals at one time. You are using one of those signals right now to wait on your networking socket (or whatever you're doing)... so you could create another signal to indicate that you have some kind of message for the thread.

The message could be stored in a variable that's protected by a mutex.

When the thread gets the signal, it looks in the variable, sees that you want it to close, and then stops itself (cleaning up any variables it needs to clean up).

It's not recommended to try and forcibly close a thread, as you seem to suggest wanting to do, because this will not deallocate variables you have (implicitly or explicitly) allocated for that thread.
Re: how to stop a thread that is waiting, e.g., listen() [message #11832 is a reply to message #11803] Thu, 27 September 2007 10:57 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
i used signals only b/t processes and only saw a tkill() sth. signaling a thread, though not yet knowing how to use it. and U++ probably cannot support this with Thread class.

furthermore, do not pthread_kill() and _cancel() send signals, too? so these functions work the same way as tkill()?
Re: how to stop a thread that is waiting, e.g., listen() [message #11835 is a reply to message #11802] Thu, 27 September 2007 13:20 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
Sorry... I have mostly worked with Windows programming, where the term 'signal' takes on a slightly different meaning, I think. At the very least, I meant the word in a far more generic way than I think you took it.

I can't really speak too clearly on POSIX-oriented programming, and I still need to peek a little more closely at how Ultimate++ handles threading in general, but while there might be commands to kill a thread, that normally means you're ending the thread before it has had a chance to clean up. Especially for long-running applications, this is not a good idea.

In general, regardless of platform, you want to somehow tell the thread, while it's running, that it should stop running, so it can clean itself up. That's all I'm trying to say.

In the Windows world, you do that through an event/signal.

I get the impression that, in the POSIX world, a signal interrupts the flow of the thread... that isn't what I'm aiming for. At least, I don't think it's what I'm aiming for... but again, I am not very experienced in POSIX. I could be completely wrong.

Somehow, while your thread waits for information to arrive on the socket, something tells the thread that it can stop waiting, and handle the network information. That same mechanism should also be able to provide a way of telling your thread that it can stop waiting, and start handling some other kind of information (in this case, checking a variable to see if the thread should quit). While I know how to do such a thing in Windows, I do not know how to do such a thing with POSIX, and I'm not entirely sure (yet) how Ultimate++ handles it... but I expect you could probably figure it out by examining the Ultimate++ code.
Re: how to stop a thread that is waiting, e.g., listen() [message #11837 is a reply to message #11802] Thu, 27 September 2007 13:43 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
Out of my own curiosity, I took a peek at Thread.h/Thread.cpp in Core, and socket.h/socket.cpp in Web.

Thread.h/.cpp detail the Thread class, a class you can use for handling multi-threaded needs in an application. It exposes a Wait function, which returns an int. The integer it returns is either the exit code for the thread, or a numeric identifier for an event that was sent to the thread (well... in Windows... in POSIX, it seems to work differently, if I am reading this correctly... pthread_join [which will return 0] or the return value of the terminated thread).

For Windows, the Socket class seems to support the use of the Event object, but that object is not exposed to POSIX, if I'm reading everything correctly.

At a guess, you could probably create a Socket, set it up for networking however you need to, and use the static 'Wait' command with a timeout to wait on network traffic. If there's traffic, do what you need to do with the traffic. Otherwise, check a variable shared between your threads to see if you need to shut down your thread.

Perhaps someone else might have a better way to do this, but this seems to be the safest way to handle Sockets, multi-threading, and keeping your application working in both Windows and POSIX environments.
Re: how to stop a thread that is waiting, e.g., listen() [message #11853 is a reply to message #11802] Fri, 28 September 2007 05:13 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
MS way IS what i want.
here's my plan to implement and hope to get some advice from ya,

in the main window thread, create a thread.
in the 2nd thread, open a network socket.
in the 1st thread, when it needs to tell sth. to or shut down the other thread, SetEvent.
in the 2nd thread, WaitForMultipleObjects on both the network socket and the event from the 1st thread.

pretty much like write() and select() on POSIX. how's this? thank you so much.
Re: how to stop a thread that is waiting, e.g., listen() [message #11881 is a reply to message #11802] Sat, 29 September 2007 14:46 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
Well, at least as of 2007.1 (I don't know about the dev builds, as I haven't been using them), it looks like you may need to create your own objects to get the behavior you're describing, at least for POSIX.

I have the impression that this area of Ultimate++ is still a little new.
icon5.gif  Re: how to stop a thread that is waiting, e.g., listen() [message #12187 is a reply to message #11881] Tue, 16 October 2007 11:59 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
then if i choose fd_set and select() way, how could i create a socket/fd between threads? MinGW does not seem to support machine's local connection.

[Updated on: Fri, 19 October 2007 11:49]

Report message to a moderator

Re: how to stop a thread that is waiting, e.g., listen() [message #12200 is a reply to message #11881] Wed, 17 October 2007 19:16 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
tvanriper wrote on Sat, 29 September 2007 08:46

Well, at least as of 2007.1 (I don't know about the dev builds, as I haven't been using them), it looks like you may need to create your own objects to get the behavior you're describing, at least for POSIX.

I have the impression that this area of Ultimate++ is still a little new.


I think that sort of problem is the fact that these thing are quite platform specific, especially if you want to have optimal solution - it is hard to support them in multi-platform library..

Mirek
icon9.gif  Re: how to stop a thread that is waiting, e.g., listen() [message #12226 is a reply to message #12200] Fri, 19 October 2007 11:52 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
ok. i understand this is rather platform specific.
does that mean i have to write separate code for win & unix and control them using macros?
Re: how to stop a thread that is waiting, e.g., listen() [message #12228 is a reply to message #11802] Fri, 19 October 2007 15:35 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
I can definitely appreciate the challenge of creating something uniform that works across both platforms, where multi-threading is concerned.

This requires some thought.

Here's what boost has been thinking, on this topic, on the odd chance it helps:

http://www.boost.org/doc/html/thread.html

[Updated on: Fri, 19 October 2007 15:49]

Report message to a moderator

Re: how to stop a thread that is waiting, e.g., listen() [message #12259 is a reply to message #12228] Mon, 22 October 2007 11:47 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
wow. boost is a good thing. wonder whether it can be used here (in U++)
Re: how to stop a thread that is waiting, e.g., listen() [message #12266 is a reply to message #11802] Mon, 22 October 2007 17:47 Go to previous messageGo to next message
tvanriper is currently offline  tvanriper
Messages: 85
Registered: September 2007
Location: Germantown, MD, USA
Member
Well, it's maintained separately, by the boost folks, but since it's just C++, I'm sure you could use it in addition to the Ultimate stuff if accomplishes your needs.

Goodness knows, I mix Ultimate++ with the standard C++ library (io streams, maps, etc) whenever I feel the need, without any adverse side effects. I haven't tried mixing with boost yet, but I can't imagine there'd be any problems.

I would heed the advice boost gave regarding event driven multitasking. They suggest that it's fraught with peril. Er, that it has a tendency to lead to some multi-tasking problems of one form or another. Basically, it has a tendency to lead to unsafe code, while some of the other approaches they recommend lead to safer practices.
icon11.gif  Re: how to stop a thread that is waiting, e.g., listen() [message #12299 is a reply to message #12266] Wed, 24 October 2007 04:20 Go to previous messageGo to next message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
yeah i heard of boost, just never used it, or even got to know it more.
speaking of my needs, i still had the tendancy to KILL the thread. so boost probably cannot fit in my project.

more considerations left later since it seems much time needed.
Re: how to stop a thread that is waiting, e.g., listen() [message #12630 is a reply to message #12299] Sun, 11 November 2007 18:49 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
bonami wrote on Tue, 23 October 2007 22:20

yeah i heard of boost, just never used it, or even got to know it more.
speaking of my needs, i still had the tendancy to KILL the thread. so boost probably cannot fit in my project.

more considerations left later since it seems much time needed.


BTW, one note: Current U++ memory allocator needs a specific call at the end of each thread to release per-thread allocation cache.

This is done automatically with U++ Thread, but has to be called explicitely if you do threads using anything else.

Mirek
Re: how to stop a thread that is waiting, e.g., listen() [message #12643 is a reply to message #12630] Mon, 12 November 2007 02:19 Go to previous message
bonami is currently offline  bonami
Messages: 186
Registered: June 2007
Location: Beijing
Experienced Member
IC. thanks. Cool
i'll use U++ thread.
Previous Topic: GuiMT example does not calculate all divisors...
Next Topic: will implement a UDP class?
Goto Forum:
  


Current Time: Thu Mar 28 16:21:51 CET 2024

Total time taken to generate the page: 0.01666 seconds