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 run a NSTimer on a thread using iPhone SDK 3.0.
I was trying to postpone adding controls to my main form, with a goal
I was trying to develop a method pipeline using asynchronous method invocation. The logic
Im trying to implement a scenario using threads to execute concurrent commands on a
I have a GUI C# application that has a single button Start/Stop. Originally this
I am trying to run a timer in the background of my application, i
I am trying to write the simples GUI countdown. I found in Internet some
I'm trying to develop a small application that tests how many requests per second
I'm having some trouble implementing DoubleBuffer into my program. Before you faint from the
I'm looking for a step-by-step explanation on how to go from the normal folder

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.