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.
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).
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.
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.