How do you use Multithreading,, Critical Section or Atomic Instructions?


I’m thinking about how to protect a piece of code in a Thread from being interrupted by other (System) Threads. I have some bits of time Critical SerialPort operations which should not be interrupted by other threads.
I already found Monitor Klasse (System.Threading) | Microsoft Learn


But I’m not sure if it will work as intended or if it is even implementable on WinCe.

Btw.: Is it possible to move the “System” Tasks to another Core to reach the maximum performance of the program? Do you have an example for this?

Best regards, Stefan

Locks, both those supported by .NET CompactFramework and those provided by the OS can be used to prevent some parts of code from being executed at the same time by multiple threads or to synchronize access to shared resources. They can’t give a thread exclusive usage of the CPU.
What you can do is to rise your thread priority to the highest level (0), using CeSetThreadPriority. This will grant that your thread will not be interrupted by other threads, unless they also run at priority 0. The thread could still be interrupted by interrupt service routines running in the kernel, but those usually don’t have a big impact on execution time.
In CE 7 and 2013 the scheduler assigns thread to all the cores, limiting a thread to a specific core is doable, but will impact its performances if the core is busy executing another thread, so, except for very specific use-cases, it’s better to let the scheduler manage threads on all the cores.
Consider also that rising priority to 0 can lead to some issues, your thread may get into an infinite loop preventing other thread in the system for running or cause a priority inversion (waiting on something owned by a low-priority thread and rising its priority) that can impact performances. Every time you call an OS API you may get into this conditions and things can get even worse on the .NET framework when the runtime may “freeze” all managed threads when doing garbage collection or other global operations.
If you give us “the big picture”, explaining what you need to do and what are the issues you are facing, we may able to provide a solution that does not involve high-priority threads or similar potentially risky operations.

My use case is to send UART Data at certain points in time.
Each transmission consists of 3 parts to send. These Parts need to be send atomic.
In 99% of the executions it seems to work out.

BTW: Is Thread.Priority more or less the same function as CeSetThreadPriority?

Are you sending all the 3 parts with a single call to the write function or in separate calls? Sending them in the same call should give a single buffer to the driver and most of its processing will be done in a high priority IST, instead of low-priority application thread.
CeSetThreadPriority allows you to work on the whole range of priorities (0-255 with 0 being the highest), the standard SetThreadPriority API and its managed Thread.Priority counterpart works only on the 8 lower levels (248-255).