Reputation: 13195
I am looking for a cross- platform C thread library to do things like:
launch a long-running thread, and in the thread, wait for an event to happen; when this event happens, do something, and then wait for the next event.
So, I don't need to do parallel computations, just simple task in another thread.
Is it appropriate to use OpenMP for this?
Upvotes: 0
Views: 76
Reputation: 9983
Yes, OpenMP is up to the task and is probably your most portable solution in C, albeit not the most elegant one in your particular use case.
OpenMP is based on the fork-join model. You will thus need to fork somewhere in your code (using the #pragma omp parallel
statement which launches threads) and then use an if
to perform a particular task on the given thread. What you want to achieve could be done by something along the lines of:
#include <omp.h>
...
#pragma omp parallel num_threads(2)
{
if (omp_get_thread_num() == 0) {
[do something]
} else {
[do something else]
}
}
With the description of your problem, you would for example perform this in your main()
and replace [do something]
by calls to various functions. One of these functions would be the loop you described in your question and the other one would be the rest of your program.
Depending on the nature of the event you talk about, you may have to define a shared variable in the #pragma omp ...
statement to perform a basic inter-thread communication.
The way you described your problem seems a bit twisted though. Wouldn't it be simpler to develop two separate programs? (Depending on the nature of the event.)
Upvotes: 2