Solved

thread-safe code in c++

Posted on 2016-08-16
2
120 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 37

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 250 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 34

Accepted Solution

by:
sarabande earned 250 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

756 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