Problem with Threads

  • Thread starter Thread starter vIndEx
  • Start date Start date
V

vIndEx

Hi! I need to use more than five threads, each one with his own priority.
Thread.Priority only allows 5 different prioritys (XP prioritys), but
Windows CE
allows 256. So, I have to use this function (Windows API) to change the
priority:

BOOL SetThreadPriority(
HANDLE hThread,
int nPriority
);
Here is my first problem: I don't have the handler of the thread. To get it,
i think I have to use this function to create the thread:HANDLE
CreateThread(
SEC_ATTRS SecurityAttributes,
ULONG StackSize,
SEC_THREAD_START StartFunction,
PVOID ThreadParameter,
ULONG CreationFlags,
PULONG ThreadId
);
Parameters
SecurityAttributes [in] Pointer to a SEC_ATTRS structure that determines
whether the returned handle can be inherited by child processes. StackSize
[in] Specifies the initial commit size of the stack, in bytes. StartFunction
[in] Pointer to the application-defined function of type SEC_THREAD_START to
be executed by the thread. ThreadParameter [in] Pointer to a single
parameter value passed to the thread. CreationFlags [in] Specifies flags
that control the creation of the thread. ThreadId [out] Pointer to a
variable that receives the thread identifier.
Now my problem is the parameter STARTFUNCTION. It's a pointer to the method,
but I can't do this in .NET. I've tried passing it a delegate of the method,
but didn't work. Any idea?

Thanks for your help
 
Passing delegates through P/Invoke is not supported by the Compact Framework. If you really need different priorities for each of your threads it is probably the easiests to use native threads. You can invoke them from managed code by creating a wrapper function in unmanaged code that in turn creates the threads and "owns" the ThreadProcs

Regards
Maarten Struys, eMV
PTS Softwar
www.opennetcf.org | www.dotnetfordevices.co

----- vIndEx wrote: ----

Hi! I need to use more than five threads, each one with his own priority
Thread.Priority only allows 5 different prioritys (XP prioritys), bu
Windows C
allows 256. So, I have to use this function (Windows API) to change th
priority

BOOL SetThreadPriority
HANDLE hThread
int nPriorit
)
Here is my first problem: I don't have the handler of the thread. To get it
i think I have to use this function to create the thread:HANDL
CreateThread
SEC_ATTRS SecurityAttributes
ULONG StackSize
SEC_THREAD_START StartFunction
PVOID ThreadParameter
ULONG CreationFlags
PULONG ThreadI
)
Parameter
SecurityAttributes [in] Pointer to a SEC_ATTRS structure that determine
whether the returned handle can be inherited by child processes. StackSiz
[in] Specifies the initial commit size of the stack, in bytes. StartFunctio
[in] Pointer to the application-defined function of type SEC_THREAD_START t
be executed by the thread. ThreadParameter [in] Pointer to a singl
parameter value passed to the thread. CreationFlags [in] Specifies flag
that control the creation of the thread. ThreadId [out] Pointer to
variable that receives the thread identifier
Now my problem is the parameter STARTFUNCTION. It's a pointer to the method
but I can't do this in .NET. I've tried passing it a delegate of the method
but didn't work. Any idea

Thanks for your hel
 
vIndEx said:
Hi! I need to use more than five threads, each one with his own priority.
Thread.Priority only allows 5 different prioritys (XP prioritys), but
Windows CE allows 256.

Out of interest, why do all your threads need different priorities? If
you need to distinguish more than 5 priorities, it sounds to me like
you're probably relying on those priorities too heavily. I personally
only ever treat priorities as a "hint" - and as such, 5 different types
of hint is more than enough.
 
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)
 
Hum... I'm not sure I've understand you... Do you mean I have to create the
methods with Visual C++ emebedded and call them with native threads? That
doesn't solve my problem, I need to use my .NET object's methods.



Maarten Struys said:
Passing delegates through P/Invoke is not supported by the Compact
Framework. If you really need different priorities for each of your threads
it is probably the easiests to use native threads. You can invoke them from
managed code by creating a wrapper function in unmanaged code that in turn
creates the threads and "owns" the ThreadProcs.
Regards,
Maarten Struys, eMVP
PTS Software
www.opennetcf.org | www.dotnetfordevices.com

----- vIndEx wrote: -----

Hi! I need to use more than five threads, each one with his own priority.
Thread.Priority only allows 5 different prioritys (XP prioritys), but
Windows CE
allows 256. So, I have to use this function (Windows API) to change the
priority:

BOOL SetThreadPriority(
HANDLE hThread,
int nPriority
);
Here is my first problem: I don't have the handler of the thread. To get it,
i think I have to use this function to create the thread:HANDLE
CreateThread(
SEC_ATTRS SecurityAttributes,
ULONG StackSize,
SEC_THREAD_START StartFunction,
PVOID ThreadParameter,
ULONG CreationFlags,
PULONG ThreadId
);
Parameters
SecurityAttributes [in] Pointer to a SEC_ATTRS structure that determines
whether the returned handle can be inherited by child processes. StackSize
[in] Specifies the initial commit size of the stack, in bytes. StartFunction
[in] Pointer to the application-defined function of type SEC_THREAD_START to
be executed by the thread. ThreadParameter [in] Pointer to a single
parameter value passed to the thread. CreationFlags [in] Specifies flags
that control the creation of the thread. ThreadId [out] Pointer to a
variable that receives the thread identifier.
Now my problem is the parameter STARTFUNCTION. It's a pointer to the method,
but I can't do this in .NET. I've tried passing it a delegate of the method,
but didn't work. Any idea?

Thanks for your help
 
Still, the Thread scheduler is not deterministic (if I understand it
correctly). Thus, depending on thread priorities for RMS may or may not
work. My experience on the desktop suggests this, and I have doubts about
it on CF.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
vIndEx said:
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)

I would suggest that developing a real-time system with the Compact
Framework (or quite possibly with WinCE in general) is unlikely to
work. You just don't get the level of control you'd really need - you
also need to consider things like garbage collection which could freeze
all threads for any length of time.
 
I guarantee you are *not* creating a real-time system with managed code.
You cannot get deterministic behavior from a system that uses a GC.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


vIndEx said:
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)
 
I'm not trying to develop a determinist real-time system, just something
that works reasonably well with non-strict conditions. However, i think I
can include the garbage collector as a task
in the system design. I still have to look for things about GC behaviour
(times, periods).




vIndEx said:
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)
 
Hi -

Microsoft terminology might be confusing here (it has confused me several
times). WinCE is a real time operating system. The WinCE.NET variety of
WinCE is as well. But that .NET addition to WinCE DOES NOT equate to the
..NET framework, C#, or VB.NET .

Tx,

- K.

vIndEx said:
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)
 
TimeSys implemented the RTSJ spec for embedded devices. So, Java qualifies
for one instance on one version of Linux.

- K.

Chris Tacke said:
I guarantee you are *not* creating a real-time system with managed code.
You cannot get deterministic behavior from a system that uses a GC.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net
 
Interesting. I'll have to take a look because I find it quite intriguing.


--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Kevin Hutchison said:
TimeSys implemented the RTSJ spec for embedded devices. So, Java qualifies
for one instance on one version of Linux.

- K.

Chris Tacke said:
I guarantee you are *not* creating a real-time system with managed code.
You cannot get deterministic behavior from a system that uses a GC.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


vIndEx said:
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)



"Jon Skeet [C# MVP]" <[email protected]> escribió en el mensaje
Hi! I need to use more than five threads, each one with his own
priority.
Thread.Priority only allows 5 different prioritys (XP prioritys), but
Windows CE allows 256.

Out of interest, why do all your threads need different priorities? If
you need to distinguish more than 5 priorities, it sounds to me like
you're probably relying on those priorities too heavily. I personally
only ever treat priorities as a "hint" - and as such, 5 different types
of hint is more than enough.
 
Hopefully to answer your original question: a thread can get its own thread
handle by pinvoking to GetCurrentThread or GetCurrentThreadId.

On the larger issues that have been raised by this thread:

While it is theoretically possible to make a real-time virtual machine
system, .NETCF was not designed for that. It does not do anything to make
it easy and a few things that will make it difficult. For example:

- At unpredictable times, any thread can decide to perform garbage
collection. All of the other threads in the app domain will be suspended
while the collection occurs. There is no constraint on how long the
collection can take (though in practice it falls within fairly regular
bounds). Since any thread can perform the collection, effectively a low
priority thread can cause the higher priority threads to wait for it.
Garbage collection only takes place if new objects are being allocated, so
theoretically it is possible to avoid it all together. However, most any
meaningful managed code will allocate objects.

- Associated with garbage collection is the "finalizer thread". After a
garbage collection, the finalizer thread calls the finalizers (~Foo) of any
unreferenced objects. Those objects cannot be actually freed until the
finalizer completes. Since the finalizer thread runs at the default normal
thread priority, if you have high priority threads constantantly running
you could "starve" the finalizer thread of execution time and prevent
objects from getting freed. I would recommend that you use
higher-than-normal priorities only for short periods of time to make sure
lower priority threads get their chance to run. Reducing the number of
objects with finalizers would also improve your approximation of real-time.

- The first time a method is called it will need to be Just-In-Time
Compiled (JITed). Generally this is not needed on subsequent calls, but
depending on the amount of JITed code in your application and the memory
useage on the device over-all, JITed methods may be discarded from memory
forcing subsequent calls to re-JIT the method. This can impose arbitrary
pauses any time you call a method. Reducing the working set (both code and
data) of your application can drastically reduce the likelyhood of your
methods being discarded but cannot guarantee it.

- A class can define a "class constructor", also known as the "class
initializer" or "static constructor". The first time a static field of a
particular class is accessed or the first time a method is called on the
class, the class constructor will run. This will only be done once per
class. Just something else that can cause a slight pause on occasion,
though since it can only happen once per class, it isn't such a problem if
you are aware of it.

For most applications, these non-deterministic pauses are too short to
notice and not an inconvenience. For most real-time applications, I would
think they would be a difficult barrier to overcome.

--------------------
From: "vIndEx" <[email protected]>
Subject: Re: Problem with Threads
Date: Tue, 2 Mar 2004 22:58:56 +0100

I'm not trying to develop a determinist real-time system, just something
that works reasonably well with non-strict conditions. However, i think I
can include the garbage collector as a task
in the system design. I still have to look for things about GC behaviour
(times, periods).




vIndEx said:
I'm developing a real-time system, with Rate Monotonic Scheduling. If you
want to learn more about this, ask Google ;)

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top