I have a socket programming problem. I am running the server and then it waits for the client. Once I run the client though, nothing happens, it just terminates and brings back the prompt. Basically it compiles alright but it doesn't run at all. It terminates as soon as I run it. This only happens when I use threads in the client code.

This is the code I'm using:

if(pthread_create(&threadID[i++], NULL, (void *)dostuff, (void *)(intptr_t)sock) != 0)  
        {
        perror("Thread create error");
        }

On the other hand, if I type in simply

dostuff(sock);

The client program does execute. I need threading because I need to implement I/O multiplexing. Could you tell me how to stop the client from terminating when I use threads?

有帮助吗?

解决方案

You'll need to wait for the thread to finish before exiting the program, for example using pthread_join

// do this before returning from main
pthread_join(threadID[i], NULL);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top