Parallel threads stopping each other
I want the following behavior in my application: while the application (server) is waiting for messages from another application (client), I want to be able to exit if it is too long with input from the user.
Therefore, I have to start two threads from the third:
-
WaitForClientMessage
-
WaitForUserInput
Using pthread
, I assumed I could call each thread and give them the id of the other, so if they run out, they override the other. But now I see that it won't work.
How to do it? I think it's easy because this behavior is often seen, but I don't know how to make it work.
EDIT Here is some generic code describing what I was imagining.
void main_thread( void)
{
void * thread_rtn_val;
/* Parallel threads */
pthread_t thread_WaitForClientMessage;
pthread_t thread_WaitForUserInput;
/* Run Threads */
pthread_create(&thread_WaitForClientMessage, NULL, run_window, (void *)thread_sdp);
pthread_create(&thread_WaitForUserInput, NULL, run_client, (void *)arg_array);
}
void run_window( void)
{
/* Refresh screen and watch for user input */
for(...)
{
if(user press enter)
{
phtread_cancel(thread_WaitForClientMessage)
}
}
}
void run_client( void)
{
/* Wait for client message */
recv()...
phtread_cancel(thread_WaitForUserInput)
}
+3
source to share