-1

I'm kind of new to multithreading, and this is a small piece of a very large homework for my operating systems class. Currently, I have a C++ struct as follows:

struct arguments {
    std::string string1;
    std::string string2;
    pthread_mutex_t bsem;
    pthread_cond_t wait = PTHREAD_COND_INITIALIZER;
    pthread_mutex_init(&bsem, NULL);
    int turn_index = 0; // To identify which thread's turn it is.
};

The line containing:

pthread_mutex_init(&bsem, NULL);

Is giving me errors, namely the two:

  • expected an identifier before &
  • expected an identifer before _null

What is a quick resolve to this? I've seen someone make a constructor for the struct object and initalized the mutex in the constructor, but why do we need that? Also, is there a way to do it without a constructor?

Thank you very much.

Ken
  • 35
  • 4
  • Can you go back and review how your C++ textbook explains that you should go about initializing a class, like that? C++ is just too complicated, and attempting to guess what the right syntax should be is unlikely to work. That's the "quick resolve" for this: use the right syntax, as shown in your textbook. Is there something in your textbook's explanation that's unclear? `struct` or `class` members must be initialized in completely different ways. Note: multiple. And as far as why you need to use constructors, that should also be explained in your textbook. – Sam Varshavchik Jul 13 '22 at 21:41
  • 1
    P.S., why are you using C-style POSIX threads, in a C++ class, instead of `std::thread`? – Sam Varshavchik Jul 13 '22 at 21:47
  • Because the fast-pace world of computer technology is woefully underserved by many curriculums. – user4581301 Jul 13 '22 at 21:50
  • @SamVarshavchik Not too sure how to respond to your first comment. We don't have a textbook. Also, I already have my `struct` initialized in the main function. As for your second comment, the original template that I am building this off used POSIX threads. That's all we've been exposed too in this class. The homework may be written in C though. – Ken Jul 13 '22 at 21:53
  • 1
    *We don't have a textbook.* Then you are being set up for failure. Without [a good guide](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to the language you are unlikely to learn C++ fast enough to keep up with C++. The Internet is not sufficient. Without a solid grounding in the fundamentals you will not know the terminology necessary to discuss the language, perform effective websearches, or tell the rantings of an idiot from the advice of an expert. – user4581301 Jul 13 '22 at 21:55

1 Answers1

3

You can't perform non-declarative statements inside of a struct declaration. What you can do is add a constructor (and destructor, in this case) that performs the extra statements you need, eg:

struct arguments {
    std::string string1;
    std::string string2;
    pthread_mutex_t bsem;
    pthread_cond_t wait = PTHREAD_COND_INITIALIZER;
    int turn_index = 0; // To identify which thread's turn it is.

    arguments() {
        pthread_mutex_init(&bsem, NULL);
    }

    ~arguments() {
        pthread_mutex_destroy(&bsem);
    }
};

That being said, if you are using C++11 or later, you should use std::mutex (and std::condition_variable) instead:

struct arguments {
    std::string string1;
    std::string string2;
    std::mutex bsem;
    std::condition_variable wait;
    int turn_index = 0; // To identify which thread's turn it is.
};

And consider using std::thread instead of pthreads.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770