473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Do I need to lock socket access when transferring data in multi-threaded application?

emibt08
25
Hi,

I have an application that I recently revised. It transfers data between 2 programs, including files as well as small data chunks. I never had a problem (got garbage at the other side) or anything like that, but I am curious whether I need to lock the socket access when i am sending. An oversimplified example of a single send would be like:

Expand|Select|Wrap|Line Numbers
  1. bool sendData(SOCKET sock, uint32_t size, const char * data)
  2. {
  3.     uint32_t nSizeNetOrder = htonl(size);
  4.     const char * pSize = reinterpret_cast<const char*>(&nSizeNetOrder);
  5.     send(sock, pSize, sizeof(uint32_t), 0);
  6.     send(sock, data, size, 0);
  7.     return true;
  8. }
  9.  
Of course, I have many checks, sending data in parts etc. But the point is that in this scenario, for every send that I make there are at least 2 calls to send. Sometimes more, if there's more data.
Now if multiple threads are trying to send data on the same socket, my logic dictates that at some point this may get corrupted. Ex, one thread sends the size, then another thread sends the size, then the 1st thread starts sending data etc. As I said, it hasn't happened before, but I would like to make sure, so maybe I should do:

Expand|Select|Wrap|Line Numbers
  1. bool sendData(SOCKET sock, uint32_t size, const char * data)
  2. {
  3.     lock();
  4.     uint32_t nSizeNetOrder = htonl(size);
  5.     const char * pSize = reinterpret_cast<const char*>(&nSizeNetOrder);
  6.     send(sock, pSize, sizeof(uint32_t), 0);
  7.     send(sock, data, size, 0);
  8.     unlock();
  9.     return true;
  10. }
  11.  
Where lock/unlock would use some OS primitives for the task (like mutex).
I would appreciate any suggestion on improving this and your oppinions.

Thank you
May 5 '10 #1

✓ answered by Banfa

That is actually a platform specific question we would need to know the OS and platform you are running on.

However since you use SOCKET I am assuming its Windows in which case the answer I believe is yes (or at least that is how I did it last and I assume I had a good reason to do it that way). Generally you should not assume anything about the multi-tasking capabilities of third party libraries and if the documentation says nothing about it then you should assume that they don't handle it.

In your second source listing I would move lock() down a couple of lines, only protect the minimum that actually requires protection because the less time you keep the lock the more easily you program will flow.

2 2740
Banfa
9,065 Expert Mod 8TB
That is actually a platform specific question we would need to know the OS and platform you are running on.

However since you use SOCKET I am assuming its Windows in which case the answer I believe is yes (or at least that is how I did it last and I assume I had a good reason to do it that way). Generally you should not assume anything about the multi-tasking capabilities of third party libraries and if the documentation says nothing about it then you should assume that they don't handle it.

In your second source listing I would move lock() down a couple of lines, only protect the minimum that actually requires protection because the less time you keep the lock the more easily you program will flow.
May 6 '10 #2
emibt08
25
Thanks for your response Banfa, that clears up the hesitation.
May 6 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: r_burgess | last post by:
I am looking for some guidance on transferring data between two pages in my ASP.net Web app (intranet). I have a form that will have a button and a text box on it (among other controls of course)...
7
by: Lyn Duong | last post by:
Hi all I'm trying to transfer data from sqlserver 2000 to ibm db2 AIX 8.1.4 using dts packages This works when I use db2 ole db drivers but the problem is that the transfer is very slow as the...
3
by: phong.lee | last post by:
Hello all, i'm new at this. I need some assistant in transferring data from excel to access. I created a macro that basically gather all the necessary data that i need to bring into access. I...
4
by: yaron | last post by:
Hi, I have a problem when sending data over TCP socket from c# client to java server. the connection established ok, but i can't send data from c# client to java server. it's work ok with...
15
by: http://www.visual-basic-data-mining.net/forum | last post by:
Does anyone have any idea how to transferring data from TextBox1 in form1 to textBox2 in form2..... That means after i fill in any data in textBox1 and click Next button... It will bring me to...
2
by: darthghandi | last post by:
I am trying to pass a socket object when an event is signaled. I have successfully bound to a network interface and listened on a port for incoming connection. I have also been able to accept...
3
by: amrit1123 | last post by:
Respected Sir.. I am working on Access Project. When I Transfer Forms of an ADP file into an .mdb database using ole function (docmd.transferdatabase( ) ) then a message...
2
hsriat
by: hsriat | last post by:
I have a page working on Ajax. The problem is, after doing many changes using Ajax (like uploading, changing name, adding to favorites etc), the status bar starts behaving unexpectedly. Even when...
10
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from...
3
by: angusfreefa | last post by:
Dear All, I am facing a problem of transferring data between 2 tables within the same database. I set up 2 tables. The first table is the permanent table (oos_table) for saving records. the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.