Solved

thread

Posted on 2011-03-04
2
470 Views
Last Modified: 2012-05-11
If 2 threads are trying to send data over TCP/IP socket connection() using the same socket, should both the thread first acquire mutex lock?

if they send() without lock will that cause data loss on receiving side which is waiting on recv()
0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 35039097
When sharing a socket between threads, you should use proper synchronization, so there's never more than one thread that tries to use the socket at the same time.

You could do that with mutexes indeed. Makes sure to lock the mutex before every operation that uses the socket, and unlock it after the operation has finished.

The risk when not doing this, is that you'll garble the data, or worse : corrupt the socket.
0
 

Author Closing Comment

by:learningunix
ID: 35039224
Thx!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question