?
Solved

thread-safe code in c++

Posted on 2016-08-16
2
Medium Priority
?
157 Views
Last Modified: 2016-08-30
I was asked to make a calculator code thread-safe.  The program is written in c++ and the IDE is Visual studio 2012.
I created a web application that references the c++ dll in order to test this.  Using Curl i can send multiple requests to the web app at the same time.
Initially before I made any changes to the calculator I was getting a mix of results (numbers) when sending multiple requests. After making some changes now the numbers look correct when doing multiple requests, but there is a section in the answer that contain messages and that section is still coming incorrect.  The messages from one request seem to show in another request and vice versa.

The Calculator is made up of several C++ classes. The changes I made so far were to remove some static variables I found in some functions.  I cannot find any more static variables.

Can anyone give some suggestions of what else I should be looking for to make this code thread-safe?

Thank you!!
0
Comment
Question by:chicho12
[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 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 1000 total points
ID: 41757760
look for thread variable protection code like a mutex

http://www.cplusplus.com/reference/mutex/mutex/

if you have different threads accessing the same variables, then you need to lock those variables before changing the data
 and preferable unlock after the change

if you don't unlock, your app will hang
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1000 total points
ID: 41758288
actually your dll Code automatically is thread-safe if you don't use neither global nor shared data for each request. shared data either could be static members or static objects (singletons) or pointers passed to more than one thread or static buffers when receiving request data or sending result data.

you should post some code where you think that any of the above rules might be violated. i think it should be the part where you receive a new request and create the thread to do the calculation and finally return the result to the client.

Sara
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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Simple Linear Regression
Six Sigma Control Plans

649 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