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 » Socket and multiple Threads
Socket and multiple Threads [message #23897] Wed, 02 December 2009 15:49 Go to next message
whiteman is currently offline  whiteman
Messages: 11
Registered: May 2009
Promising Member
Is possible to use the same socket in more than one thread? I have a program that open more connection and wants to redirect all output to the same socket, this can create some problem with Upp Socket?
Thanks!
Re: Socket and multiple Threads [message #23905 is a reply to message #23897] Fri, 04 December 2009 00:24 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
No problem,
just as long as you know what you are doing with them and that you have a protection mutex for each socket (the best way) or for all the sockets

Re: Socket and multiple Threads [message #27309 is a reply to message #23897] Sun, 11 July 2010 23:50 Go to previous messageGo to next message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

Hello, everyone!

Is there any example for this topic? In my program, when I try to use the Socket in threads, Socket is always closed. One variable: in main thread it's open, in other threads - closed. Why so?

Thank you,
Anton.
Re: Socket and multiple Threads [message #27310 is a reply to message #27309] Mon, 12 July 2010 00:48 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Sc0rch wrote on Sun, 11 July 2010 17:50

Hello, everyone!

Is there any example for this topic? In my program, when I try to use the Socket in threads, Socket is always closed. One variable: in main thread it's open, in other threads - closed. Why so?

Thank you,
Anton.


Hey, it is still the same story (as with SQL).

As soon as you have multiple threads and shared object, you HAVE TO "serialize" the access to the object using the mutex, so that only single thread "runs" the object at any given time. I mean, only single thread at any given time can read/write/execute members of object. This is what mutexes are for.

Rules are more fine-grained, but in practice this pretty much sums it.

Note: There ARE some objects where this does not hold, where you can call methods without mutex locking. Most of them are associated with multithreading, e.g. Mutex class itself... Smile
Re: Socket and multiple Threads [message #27312 is a reply to message #27310] Mon, 12 July 2010 08:03 Go to previous messageGo to next message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

No Message Body

[Updated on: Mon, 12 July 2010 16:15]

Report message to a moderator

Re: Socket and multiple Threads [message #27315 is a reply to message #27312] Mon, 12 July 2010 09:01 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Sc0rch wrote on Mon, 12 July 2010 02:03

luzr wrote on Mon, 12 July 2010 05:48

Sc0rch wrote on Sun, 11 July 2010 17:50

Hello, everyone!

Is there any example for this topic? In my program, when I try to use the Socket in threads, Socket is always closed. One variable: in main thread it's open, in other threads - closed. Why so?

Thank you,
Anton.


Hey, it is still the same story (as with SQL).

As soon as you have multiple threads and shared object, you HAVE TO "serialize" the access to the object using the mutex, so that only single thread "runs" the object at any given time. I mean, only single thread at any given time can read/write/execute members of object. This is what mutexes are for.

Rules are more fine-grained, but in practice this pretty much sums it.

Note: There ARE some objects where this does not hold, where you can call methods without mutex locking. Most of them are associated with multithreading, e.g. Mutex class itself... Smile


Yes, I know about Mutex from last thread. I'm using it for socket. Problem is: when I open static Socket in main loop (console app), and try to read it from thread, I'm getting ASSERT(IsOpen ... ). IsOpen for this Socket returns different results: in main thread - true, in other thread - false. I'm comfused about this, sorry if the reasons are stupid.

Sorry for my English,
Anton


Is not it possible that the thread gets to read it first?

If that seems OK, a little testcase would help...

Mirek
Re: Socket and multiple Threads [message #27327 is a reply to message #23897] Mon, 12 July 2010 16:20 Go to previous message
Sc0rch is currently offline  Sc0rch
Messages: 99
Registered: February 2008
Location: Russia, Rubtsovsk
Member

I make the testcase and it works good, socket is open in all threads. Sorry me for the waste of your time! I'll try to test more, before writing in threads.
Previous Topic: SSL patch
Next Topic: Stop Download
Goto Forum:
  


Current Time: Thu Mar 28 10:17:41 CET 2024

Total time taken to generate the page: 0.01166 seconds