eouti
eouti

Reputation: 5658

C++ threads & infinite loop

I have a little problem, I wrote a program, server role, doing an infinite loop waiting for client requests.
But I would like this program to also return his pid.
Thus, I think I should use multithreading.
Here's my main :

int main(int argc, char **argv) {

    int pid = (int) getpid();
    int port = 5555

    ServerSoap *servsoap;
    servsoap = new ServerSoap(port, false);
    servsoap->StartServer(); //Here starts the infinite loop

    return pid; //so it never executes this
}

If it was bash scripting I would add & to run it in background.
Shall I use pthread ? And how to do it please ?

Thanks.
eo

Upvotes: 3

Views: 1942

Answers (4)

Neil
Neil

Reputation: 5780

Your main is attempting to do what your server should do. You're confusing a couple patterns here.

Pattern #1: Daemon

Think of the main as the program that, when on, accepts client requests and performs operations with them. The main has to wait for requests if this is the structure of the program. When a request is received, only then do you perform the requested operation. The main serves only to turn on or off this service. Normally this type of behavior is handled by default with threads. The listener activates a thread calling specific methods with information regarding the request, for instance. Unless you require threads for the work you need done, you shouldn't require threads for this.

Pattern #2: Tool

Alternatively, you could simply call this program as a tool. You'd still need a web service, but this program could be separate from that. Apart from what your tool should do, you shouldn't require threads for this.

In either case, I don't think what you're looking for is to implement threading. You're simply activating a server which does nothing. You should probably look into adding request handlers instead.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409176

If you put the infinite loop in a separate thread, and then return from main it will kill the whole process including your new thread. One solution, keeping to threads, is to make a detached thread. A better solution is probably to create a new process:

int main()
{
    int pid = fork();
    if (pid == -1)
        perror("fork");
    else if (pid == 0)
    {
        ServerSoap serversoap(5555, false);
        serversoap.StartServer();
    }

    return pid;
}

Edit: Also note the limit to the return value from main as noted in the answer from ecatmur.

Upvotes: 1

aisbaa
aisbaa

Reputation: 10633

I have a feeling that you're trying to implement daemon.

To add to @ecatmur answer, if no error has happened program should always return 0 on termination.

PID is usually saved in some file, often times in /var/run/ directory. Some programs use /tmp/ directory.

Upvotes: 1

ecatmur
ecatmur

Reputation: 157354

When a program returns (exits), all running threads terminate, so you can't have a background thread continue to run.

In addition, the int return value of main is (usually) truncated to a 7-bit value, so you don't have enough space to return a full pid.

It'd be better just to print the pid to stdout using printf.

Upvotes: 4

Related Questions