1

I have a project and I want to convert it to multi-threaded application. What are the things that can be done to make it a multi threaded application

List out things to be done to convert into multithreaded application
e.g mutex lock on shared variables.
I was not able to find a question which list all those under single hood. project is in C

ashmish2
  • 2,885
  • 8
  • 40
  • 54
  • 2
    If your application only has a single thread then you don't need to worry about making it thread safe. – Nick Feb 25 '11 at 14:06
  • Start by reading http://blogs.msdn.com/b/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx – Jon Skeet Feb 25 '11 at 14:06
  • i didnt get ur point. I want my application to multi threaded. I have taken any precaution till now to make it thread safe – ashmish2 Feb 25 '11 at 14:07
  • If it only has a single thread then it is, by definition, thread-safe. If you want to add threads, then you need to decide what the rules of engagement are and what you [actually mean by thead safety](http://blogs.msdn.com/b/ericlippert/archive/2009/10/19/what-is-this-thing-you-call-thread-safe.aspx). – David Heffernan Feb 25 '11 at 14:07
  • ok let me change the question a bit – ashmish2 Feb 25 '11 at 14:11
  • 1
    You're going to need to create some threads in order to make it multi-threaded. And then you are going to need to make sure that the resulting code is correct, where you define correct. If that sounds impossibly general and vague, then it is merely a reflection of the question. – David Heffernan Feb 25 '11 at 14:16
  • See i have a program (say server) and multiple programs are going to call this to perform some function. There fore to handle these connection and to make sure there shouldn't be any unwanted changes in variable, database etc. i have to make changes to my program. I want that changes or precaution to list out – ashmish2 Feb 25 '11 at 14:23
  • 1
    @honeybadger One easy option is to keep all state private to each connection. If you do that then you may have very little to do. Use the stack, it's your best friend. – David Heffernan Feb 25 '11 at 14:36

4 Answers4

3

Single threaded application need not be concerned about being thread safe.

This issue arises when you have multiple threads which are trying to access a commonly shared resource. At that time, you must be concerned.

So, no need to worry.


EDIT (after question been edited ) :

You need to go through the following links.

Also a good advice for converting single to multithreaded application.Check out.

Community
  • 1
  • 1
Saurabh Gokhale
  • 53,625
  • 36
  • 139
  • 164
1

The big issue is that, in general, when designing your application it is very difficult to choose single thread and then later on add multi-threading. The choice is fundamental to the design idioms you are going to strive towards. Here's a brief but poor guide of some of the things you should be paying attention towards and how to modify your code (note, none of these are set in stone, there's always a way around):

  1. Remove all mutable global variables. I'd say this goes for single threaded applications too but that's just me.
  2. Add "const" to as many variables as you can as a first pass to decide where there are state changes and take notes from the compilation errors. This is not to say "turn all your variables to const." It is just s simple hack to figure out where your problem areas are going to be.
  3. For those items which are mutable and which will be shared (that is, you can't leave them as const without compilation warnings) put locks around them. Each lock should be logged.
  4. Next, introduce your threads. You're probably about to suffer a lot of deadlocks, livelocks, race conditions, and what not as your single threaded application made assumptions about the way and order your application would run.
  5. Start by paring away unneeded locks. That is, look to the mutable state which isn't shared amongst your threads. Those locks are superfluous and need to go.
  6. Next, study your code. At this point, determining where your threaded issues are is more art than science. Although, there are decent principals about how to go about this, that's about all I can say.

If that sounds like too much effort, it's time to look towards the Actor model for concurrency. This would be akin to creating several different applications which call one another through a message passing scheme. I find that Actors are not only intuitive but also massively friendly to determining where and how you might encounter threading issues. When setting up Actors, it's almost impossible not to think about all the "what ifs."

Personally, when dealing with a single threaded to multi threaded conversion, I do as little as possible to meet project goals. It's just safer.

wheaties
  • 35,646
  • 15
  • 94
  • 131
1

This depends very heavily on exactly how you intend to use threads. What does your program do? Where do you want to use threads? What will those threads be doing?

You will need to figure out what resources these threads will be sharing, and apply appropriate locking. Since you're starting with a single-threaded application, it's a good idea to minimize the shared resources to make porting easier. For example, if you have a single GUI thread right now, and need to do some complex computations in multiple threads, spawn those threads, but don't have them directly touch any data for the GUI - instead, send a asynchronous message to the GUI thread (how you do this depends on the OS and GUI library) and have it handle any changes to GUI-thread data in a serialized fashion on the GUI thread itself.

As general advice, don't simply add threads willy-nilly. You should know exactly which variables and data structures are shared between threads, where they are accessed, and why. And you should be keeping said sharing to the minimum.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
-2

Without a much more detailed description of your application, it's nearly impossible to give you a complete answer.

It will be a good idea to give some insight in your understanding of threading aswell.

However, the most important is that each time a global variable is accessed or a pointer is used, there's a good chance you'll need to do that inside of a mutex.

This wikipedia page should be a good start : http://en.wikipedia.org/wiki/Thread_safety

krtek
  • 26,334
  • 5
  • 56
  • 84