Vikram Ranabhatt
Vikram Ranabhatt

Reputation: 7610

How to signal a thread in linux?

I am working on multi-threaded application where I am using SetEvent() to close the thread on windows.

What is the equivalent in linux c++ to send a signal to a thread? The application has to run on both windows and linux.

In windows, I am doing the following:

In the UI I have a button say Close thread. Once the user press the button it will set the event using SetEvent() which causes the thread to stop it self.

void closethread()
{
    Setevent(hEventhandle);
}


void * threadProc(void* args)
{
    waitforsingleobject(hEventhandle,infintie)
}

Is there any better to way close the thread when the user presses the button?

Upvotes: 1

Views: 1309

Answers (2)

Björn Pollex
Björn Pollex

Reputation: 76778

If you are writing a cross-platform GUI application, you probably already use some cross-platform GUI framework, like Qt or WxWidgets. These frameworks usually come with their own cross-platform libraries for all sorts of things, including multi-threading. So instead of using native APIs, check out the documentation of the framework you (hopefully) use.

Alternatively, you can consider using Boost.Thread to handle the multi-threading in a portable way.

Upvotes: 3

Erik
Erik

Reputation: 91270

You could use a condition variable - see e.g. pthread_cond_wait

Upvotes: 2

Related Questions