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  |
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 #27310 is a reply to message #27309] |
Mon, 12 July 2010 00:48   |
 |
mirek
Messages: 14255 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... 
|
|
|
|
Re: Socket and multiple Threads [message #27315 is a reply to message #27312] |
Mon, 12 July 2010 09:01   |
 |
mirek
Messages: 14255 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... 
|
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
|
|
|
|
Goto Forum:
Current Time: Sat Apr 26 14:37:10 CEST 2025
Total time taken to generate the page: 0.01052 seconds
|