Solved

thread-safe code in c++

Posted on 2016-08-16
2
115 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
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 33

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

Suggested Solutions

Title # Comments Views Activity
Advice on Xojo as a development tool over VB. 4 71
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 53
Indy 10 not Receiving UDP broadcast 3 22
C++ Code Issue 4 25
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

839 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