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 increase thread priority in pthreads?

The default Linux scheduling policy is SCHED_OTHER, which have no priority choice but a nice level to tweak inside the policy. You’ll have to change to another scheduling policy using function pthread_setschedparam (see also man sched_setscheduler) ‘Normal’ scheduling policies: (from sched_setscheduler(2)) SCHED_OTHER the standard round-robin time-sharing policy; SCHED_BATCH for “batch” style execution of processes; and … Read more

How to return a value from pthread threads in C?

Here is a correct solution. In this case tdata is allocated in the main thread, and there is a space for the thread to place its result. #include <pthread.h> #include <stdio.h> typedef struct thread_data { int a; int b; int result; } thread_data; void *myThread(void *arg) { thread_data *tdata=(thread_data *)arg; int a=tdata->a; int b=tdata->b; int … Read more

Preemptive threads Vs Non Preemptive threads

No, your understanding isn’t entirely correct. Non-preemptive (aka cooperative) threads typically manually yield control to let other threads run before they finish (though it is up to that thread to call yield() (or whatever) to make that happen. Preempting threading is simpler. Cooperative threads have less overhead. Normally use preemptive. If you find your design … Read more

Pthreads vs. OpenMP

Pthreads and OpenMP represent two totally different multiprocessing paradigms. Pthreads is a very low-level API for working with threads. Thus, you have extremely fine-grained control over thread management (create/join/etc), mutexes, and so on. It’s fairly bare-bones. On the other hand, OpenMP is much higher level, is more portable and doesn’t limit you to using C. … Read more

pthreads mutex vs semaphore

semaphores have a synchronized counter and mutex’s are just binary (true / false). A semaphore is often used as a definitive mechanism for answering how many elements of a resource are in use — e.g., an object that represents n worker threads might use a semaphore to count how many worker threads are available. Truth … Read more

POSIX threads and signals

What’s the best way to control which thread a signal is delivered to? As @zoli2k indicated, explicitly nominating a single thread to handle all signals you want handled (or a set of threads each with specific signal responsibilities), is a good technique. What is the best way to tell another thread (that might actually be … Read more

Is it possible to determine the thread holding a mutex?

You can use knowledge of the mutex internals to do this. Ordinarily this wouldn’t be a very good idea, but it’s fine for debugging. Under Linux with the NPTL implementation of pthreads (which is any modern glibc), you can examine the __data.__owner member of the pthread_mutex_t structure to find out the thread that currently has … Read more

pthread function from a class

You can’t do it the way you’ve written it because C++ class member functions have a hidden this parameter passed in. pthread_create() has no idea what value of this to use, so if you try to get around the compiler by casting the method to a function pointer of the appropriate type, you’ll get a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)