Can I get Unix’s pthread.h to compile in Windows?

pthread.h is a header for the Unix/Linux (POSIX) API for threads. A POSIX layer such as Cygwin would probably compile an app with #include <pthreads.h>. The native Windows threading API is exposed via #include <windows.h> and it works slightly differently to Linux’s threading. Still, there’s a replacement “glue” library maintained at http://sourceware.org/pthreads-win32/ ; note that … Read more

How to create a thread?

The following ways work. // The old way of using ParameterizedThreadStart. This requires a // method which takes ONE object as the parameter so you need to // encapsulate the parameters inside one object. Thread t = new Thread(new ParameterizedThreadStart(StartupA)); t.Start(new MyThreadParams(path, port)); // You can also use an anonymous delegate to do this. Thread … Read more

Using a global dictionary with threads in Python

Assuming CPython: Yes and no. It is actually safe to fetch/store values from a shared dictionary in the sense that multiple concurrent read/write requests won’t corrupt the dictionary. This is due to the global interpreter lock (“GIL”) maintained by the implementation. That is: Thread A running: a = global_dict[“foo”] Thread B running: global_dict[“bar”] = “hello” … Read more

Is recent GTK 3.22 still Boehm GC friendly (thread issue)?

Gtk does not call any signal handler from a non-main thread. Any worker thread you found in a Gtk application interacts with the main thread via a message queue. You can see that in the Glib source. For example, see the implementation of g_dbus_connection_signal_subscribe() and schedule_callbacks() in gdbusconnection.c.(A worker thread calls g_source_attach(…, subscriber->context), where the … Read more

How to test methods that call System.exit()?

Indeed, Derkeiler.com suggests: Why System.exit() ? Instead of terminating with System.exit(whateverValue), why not throw an unchecked exception? In normal use it will drift all the way out to the JVM’s last-ditch catcher and shut your script down (unless you decide to catch it somewhere along the way, which might be useful someday). In the JUnit … Read more

how to set CPU affinity of a particular pthread?

This is a wrapper I’ve made to make my life easier. Its effect is that the calling thread gets “stuck” to the core with id core_id: // core_id = 0, 1, … n-1, where n is the system’s number of cores int stick_this_thread_to_core(int core_id) { int num_cores = sysconf(_SC_NPROCESSORS_ONLN); if (core_id < 0 || core_id … Read more

Run code on UI thread in WinRT

It’s easier to directly get the CoreWindow from the non-UI thread. The following code will work everywhere, even when GetForCurrentThread() or Window.Current returns null. CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, <lambda for your code which should run on the UI thread>); for example: CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Your UI update code goes here! }); You’ll need to reference Windows.ApplicationModel.Core … Read more