?
Solved

thread-safe code in c++

Posted on 2016-08-16
2
Medium Priority
?
145 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Progress
Suggested Courses

765 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