c++ - Separating Class Functions From Rest of Class Definition -


i have school assignment involving multi-threaded problem. i'm not here ask implementation of specific problem, because haven't gotten there yet. assignment, must define few classes descend provided thread class. definition, have main file (thread.cpp), header file (thread.h), , support file containing functions called classes defined in thread.cpp (thread-support.cpp).

thread.h looks this:

#ifndef _thread_h #define _thread_h  #include <stdbool.h> #include "threadclass.h"  mutex potlock; mutex mamalock; semaphore sleepymama("sleepymama", 0); bool awake = false;  class mama : public thread {     public:         mama();      private:         void threadfunc();         void goto_sleep();         void food_ready(); };  class baby : public thread {     public:         baby(int id, mutex* pots);      private:         void threadfunc();         int id;         mama mama;         mutex *pots;         void ready_to_eat();         void finish_eating(); };  #endif 

thread.cpp looks this:

#include <iostream> #include <stdio.h> #include <string.h> #include "thread.h" using namespace std;  baby::baby(int id, mutex feedpots[]) :           id(id), pots(feedpots) {     threadname.seekp(0, ios::beg);     threadname << "baby" << id << '\0'; }  void baby::threadfunc() {     thread::threadfunc();     mamalock.lock();     if (!awake) {         sleepymama.signal();     }     mamalock.unlock();     exit(); }  mama::mama() {     threadname.seekp(0, ios::beg);     threadname << "mama" << '\0'; }  void mama::threadfunc() {     char buf[200];     thread::threadfunc();     sprintf(buf, "mama started\n");     write(1, buf, strlen(buf));      goto_sleep();      sprintf(buf, "mama woke up!\n");     write(1, buf, strlen(buf)); } 

and thread-support.cpp looks this:

#include <stdbool.h> #include "thread.h"  void mama::goto_sleep() {     mamalock.lock();     awake = false;     mamalock.unlock();     sleepymama.wait(); } 

currently, i'm using makefile compiles each of .cpp files .o files using g++ flags -g , -02 (because that's our example file had) along other flags make work our ancient custom threading library. executable compiles command c++ [custom library flags] -g -02 -o prog4 thread.o thread-support.o thread-main.o thread-main.cpp being file main function in it, includes thread.h. mutexes, semaphore, , bool in thread.h need accessible both thread.cpp , thread-support.cpp, if include thread.h in both files, multiple definition issues. multiple definition issues in thread-main well.

thread.cpp should define these variables:

mutex potlock; mutex mamalock; semaphore sleepymama("sleepymama", 0); bool awake = false; 

while thread.h should declare them:

extern mutex potlock; extern mutex mamalock; extern semaphore sleepymama; extern bool awake; 

you should (well, in cases) define variables in .cpp files. headers should contain forward declarations make symbols visible other modules.

edit

to have these simbols available in thread-support.cpp, can either:

1) #include thread.h

2) declare them manually - in thread-support.cpp add:

extern mutex potlock; extern mutex mamalock; extern semaphore sleepymama; extern bool awake; 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -