Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1034931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:28:08+00:00 2026-05-16T14:28:08+00:00

I am trying to start a method from my main() as a new thread

  • 0

I am trying to start a method from my main() as a new thread with pthread:

int main(int argc, char** argv) {
    pthread_t shipGeneratorThread;
    Port portMelbourne;

    pthread_create(&shipGeneratorThread, NULL, portMelbourne.generateShips(), NULL);

    return (EXIT_SUCCESS);
}

The Port class has a function that generates a ship:

void Port::generateShips() {
    //Generate 1 cargo ship every 2.5 hours
    bool stop = false;

    while(!stop) {
        if(numberOfShipsInBay < 20) {
            Ship ship;
            ship.setTicket(numberOfShipsInBay);
            shipsWaitingToDock[numberOfShipsInBay] = ship;
            term.displayMessage("A new ship has entered the port");
            numberOfShipsInBay++;
        } else {
            term.displayMessage("A ship has been sent to another port");
        }
        usleep(FIVE_MINUTES * 30); //2.5 hours simulated time
    }
}

But the compiler gives me an error, “invalid use of void expression” for the pthread create function.

I am new to C++ and threading, any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:28:08+00:00Added an answer on May 16, 2026 at 2:28 pm

    you should use static method in this case and yes, look into man pthread_create.
    Signature of functiona is significant.

    Also if you create thread way your code show it will be terminated as soon as main() exits.
    You need to wait for thread to accomplish. I put example below. It is not ideal but seems good enough for demonstration.

    Please pay attention to static -> non-static method transition while handling thread start. It is common approach (although not the only one possible).

    #include <stdio.h>
    #include <pthread.h>
    
    class Test {
        public:
            static void* Run(void* me) {
                static_cast<Test*>(me)->RunMe();
                return NULL;
            }
    
        private:
            void RunMe() {
                printf("%p: %p is running.\n", pthread_self(), this);
            }
    };
    
    int main() {
    
        pthread_t tid;
        Test test;
    
        printf("%p: creating thread for %p is running.\n", pthread_self(), &test);
        pthread_create(&tid, NULL, Test::Run, &test);
        printf("%p: waiting %p to finish.\n", pthread_self(), &test);
    
        // In real code you should check 'create' operation result.
        pthread_join(tid, NULL);
        printf("%p: OK, exiting\n", pthread_self());
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to start a service from the command line using net start
I'm trying to start a service that has a argument that specifies a port
I am trying to start a new MVC project with tests and I thought
I am trying to access the Cache in the Application_Start method of Global.asax. I
I'm trying to start a service as a user and things work fine, until
I'm trying to start using LINQ and specifically LINQ to SQL but I'm having
I am actually trying to start a process for winzip and zip a folder.
I'm trying to start a program using QProcess (Qt4.4 on vista64bit, developing in MSVC2005).
Why do I get following error when trying to start a ruby on rails
I have a simple WPF application which I am trying to start. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.