I’m
pretty good with threads, and what you’re describing is rather simple.
In fact, it’s so simple that I’d suggest using the easiest
available function for creating a thread without any need for a big library to
wrangle threads. Personally, I have no problem with the Win32 function to
create a thread, since it will simply call the single hard-coded entry point.
The
DLL called by LabView would contain a call to CreateThread (0,0,
&entrypoint, 0,0,0);
This
function returns right away, and meanwhile ‘entrypoint’ is called
on the new thread.
To
communicate, use a Queue class that has locks to provide for two-threaded
access. I think I saw something about “shared” collections in
Boost or TR2 or C++0x or something. If that’s easy, just use that.
Otherwise,
since what you want is fairly simple, make a STL queue class (a deque class
used in such a way that you always add on one side and remove from the other)
and the simplest possible mutex mechanism around the calls so they don’t
step on each other. Ah, but here’s where it gets more complex: if the
queue is empty you want the worker thread to sleep not keep spinning idly.
Then again, maybe you don’t care, and this is indeed very simple: Look
for a “Critical Section” or “Mutex” or “Lock”
of your choice. The fastest one in the Win32 API is created with InitializeCriticalSection,
and looking that up will give you the other calls.
To
terminate the worker, you send it a command that tells it to stop looping, and
it returns from the ‘entrypoint’ function instead.
From:
boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Francisco
Javier Porras Gálvez
Sent: Sunday, June 06, 2010 12:12 PM
To: boost-users@lists.boost.org
Subject: [Boost-users] [thread] Is Boost suitable for this application?
Hi,
all.
I
don't know anything about Boost, neither OpenMP, pthread nor Windows API thread
library. So, before begin learning one of these, I'd like to explain to you how
I want my application to work.
It
deals with running two parallel loops, one in LabView, and the other in C++. It
doesn't matter if you don't know anything about LabView, this explanation is
easy enough to understand. Attached you can see a picture which shows the
desired execution behaviour. I want to know whether my application would be
possible to achieve using Boost, or using another API. In another general C++
forum, I was suggested to use Boost.
First,
the execution begins in LabView. Here, I call a dll function written in C++.
This is the "initialization" function. Its aim is to create another
thread that will execute the main loop in C++. Then, this
"initialization" function ends, because it has to return the control
execution to LabView, but the new thread which has just been created must
continue executing in its own loop.
Now
we have two loops in parallel: LabView and C++. In LabView's loop, we call
periodically another dll function that has to send and retrieve data from the
independent thread in C++. This function, like the other, has to end executing
to pass control to LabView again, after it has done its purpose.
This
will be the behaviour until we call from LabView another dll function to stop
the independent thread (not shown in the picture). After this, the LabView
program ends.
Is
it possible to use Boost for this? If so, could you post a brief example about
how you'd make it?
Thanks
in advance,
Francisco