1

I'm developing a project that involves the interaction between three threads. The code of the the thread functions is very long, and put everything in a file does not seem a good idea, because it becomes unmanageable.

Compile separately the threads functions and then link everything is a solution that makes sense?

Obviously there are a lot of data structures shared between threads.

What is the proper way to separate the project into more files?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Luc
  • 11
  • 1

2 Answers2

1

Compile separately the threads functions and then link everything is a solution that makes sense?

Generally speaking: Yes.

Put the common stuff (like types and defines) into header file(s) (.h), which then get included by the modules (.c) that need them.

Each module also gets it's separat header file providing the prototype(s) for its function(s).

alk
  • 69,737
  • 10
  • 105
  • 255
  • Can be considered a thread as a module, even if a single thread is meaningless without the other two? – Luc Jun 28 '15 at 15:06
  • @Luc: So replace "module" by the C specific term "translation unit". – alk Jun 28 '15 at 15:36
0

Here is something I recently did, which had similar requirements as yours. The approach I followed might be of help to you. It was done in c++ but shouldn't be a problem to implement the whole thing in c.

  1. Generally the main of the application is run in a separate file and the file doesn't have much beyond main (except some static global constants).

  2. Then there's a manager class (or struct). So all the main does is create an instance of that manager class and run it. The manager had private class instances (one for each thread), created during the run method. In my case, the functionality was clearly separate so I implemented them in separate source files, with very limited interface exposed to caller manager class. The idea being one can extend the functionality very easily without the manager having to know it.

  3. If the shared data has got any more operations, I'd club that with shared data structures and implement in separate files. A pointer (or a vector or whatever) of those shared data structured is available in each of the class associated with threads.

But both 2 and 3 are defined by what you are doing in each of the threads if most of it's identical, you might want to club those together, but still prefer to keep the shared data separate in a file. Atleast helps one in logically structure the code in ones head. In your case if you want to implement in c and you end up doing lot of duplication, you might be better off then having all thread specific implementation in one file and using static functions (for information hiding from rest of the world).

Having said so - you might want to first start with a couple of files
- a driver - one implementing main and - another implementing desired functionality (for both threads may be) and then start separating out things as you start adding more code. This will help you get started writing some code without spending a lot of time thinking about how to go about organizing it. Remember it's usually hard to get things right first time, so a worthwhile idea to start with something that works and then moving things around.

Also a good idea to always start writing a Makefile. Building things when you move things around would then be a lot easier.

Hope that helps.

gabhijit
  • 3,345
  • 2
  • 23
  • 36