pthreads

How can I reverse-map (hash) `pthread_t`s to structure pointers?

I have a thread datatype in the interpreter implementation for a programming language I am working on. For various reasons, it’s a fairly common operation, to need to get the current thread (which is, itself, a pointer: a struct thread*). However, pthread_self(3) hands me a pthread_t, which is an opaque type; on some systems, it seems t...

pthreads’ manpages really don’t cover very much; where can I find more info?

The pthread_* manpages are really, really sparse in lots of areas; for instance, for all I can tell, the various pthread_attr_set* are completely undocumented — that is, I can’t figure out what each of the various types of attributes actually do! The only other useful sources of information I’ve found, than the manpages, are the famous ...

pthread_setschedprio() fails with "EINVAL"

I just started with pthreads and don't know much about it. I was trying to set the priorities of pthreads using pthread_setschedprio() but it returns "EINVAL". Here is my code: #include <pthread.h> #include <errno.h> void *Run(void *ptr); class Thread { public: pthread_t myPthread; void start() { pthread_create( &myP...

Suspend pthreads without using condition

I want to suspend pthreads but apparently, there is no such function as pthread_suspend. I read somewhere about suspending pthreads using mutexes and conditions and used it as following: #include <pthread.h> class PThread { public: pthread_t myPthread; pthread_mutex_t m_SuspendMutex; pthread_cond_t m_ResumeCond; void start() { pthread...

ICC 11.1 has strange behaviour regarding PTHREADS on ia64

Hi! I'm working on a ia64-machine using ICC 11.1. The following program compiles nicely: #include <pthread.h> #include <iostream> using namespace std; int main() { cout << PTHREAD_STACK_MIN << '\n'; return 0; } When I compile it with icc test.cpp -o test BUT when I change the contents of the file to to: #include <pthread.h> ...

sig in pthread_kill(pthread_t thread, int sig)

What does sig in int pthread_kill(pthread_t thread, int sig) refer to? Are there predefined sigs? :S ...

Compiling with mpicc (MPICH2) and pthread_barrier (-lpthread)

I have a functioning implementation of a MPI routine, which works fine. In the process of making this a hybrid between MPI and shared memory, I am using pthreads. This in turn proofed the need of pthread_barriers. But when I try to compile my code with the mpicc compiler, it complains over pthread_barrier_t and other barrier commands. ...

Help using semphores and threads

I am using the pthread library to simulate a threaded buffer. I am also using semaphores as a solution to accessing critical section variables one at a time. The main problem is that the producer is filling the entire buffer and the consumer is then emptying the entire buffer. Is this code correct? I was assuming that the production a...

Passing char[N] as thread argument in c?

Heres my code: void *PrintLine(void *line) { printf("Line: #%s\n", (char *)line); pthread_exit(NULL); } int main (int argc, char *argv[]) { char line[80]; while(fgets(line,sizeof(line),fp)) { pthread_create(&threads[rt], NULL, PrintLine, (void*)line); } fclose(fp); } Please dont tell me that runnin...

pthreads with real time priority

I need to manage a pool of threads having different priorities, so I wrote the following thread startup procedure: static int startup(thrd_t *thrd, thrd_sync_t *sync, int prio) { pthread_attr_t attr; int err; struct sched_param param = { .sched_priority = prio }; assert(pthread_attr_init(&attr) == 0); as...

Does guarding a variable with a pthread mutex guarantee it's also not cached ?

Consider a simple (global in my case) variable: int i; Somewhere this variable is accessed pthread_mutex_lock(i_mutex); if(i == other value) { do_something(); } pthread_mutex_unlock(i_mutex); Another thread updates i while it holds i_mutex . Could the compiler cache the value of i so I don't get the recent value ? Must i be vola...

Source code of PThread Library?

Hi, I am trying to find the source code of pthread library. (I guess its a supposed to be a part of Linux source code) But somehow can't find any good website that has it. I like this website: http://lxr.linux.no/#linux+v2.6.34.1/ where I usually find what I need. Somehow pthread source is not searchable. Anyway, I wanted to mention t...

pthread_mutex_lock and unlock

i have two threads, they run pretty fast, i'm using pthread_mutex_lock and pthread_mutex_unlock to access global (externed variables) data the problem is that my application takes about 15-20% of CPU running on Ubuntu Linux, the same code but with EnterCriticalSection and LeaveCriticalSection and running on Windows uses 1-2% of CPU ...

Help required with pthreads debugging

Hi, I have a server-client program in which there are multiple threads in both the server and client. There are variable number of clients and servers (like 3 servers (replicas), 10 clients). I am debugging a source file in this program. I think there is some kind of deadlock, possibly the following: A mutex lock is already held by a se...

Does usleep create thread cancellation point?

According to the Linux manpages, only the following functions are thread cancellation points: pthread_join, pthread_cond_wait, pthread_cond_timedwait, pthread_testcancel, sem_wait, sigwait. In my test program, thread exits on usleep. Thread function: void* ThreadFunction(void* arg) { int n = 0; pthread_setcancelstate(PTHREAD_C...

What are the problems with this producer/consumer implementation?

So I'm looking at using a simple producer/consumer queue in C++. I'll end up using boost for threading but this example is just using pthreads. I'll also end up using a far more OO approach, but I think that would obscure the details I'm interested in at the moment. Anyway the particular issues I'm worried about are Since this code is...

How to allow certain threads to have priority in locking a mutex use PTHREADS

Hi, Assume that the following code is being executed by 10 threads. pthread_mutex_lock(&lock) Some trivial code pthread_mutex_unlock(&lock) For purpose of explanations lets say the threads are T1, T2, T3.....T10. My requirement is that as long as T1 or T2 or T3( i.e any of T1, T2 or T3) is waiting for acquiring a lock, the other thre...

Segmentation fault in libcurl, multithreaded

So I've got a bunch of worker threads doing simple curl class, each worker thread has his own curl easy handle. They are doing only HEAD lookups on random web sites. Also locking functions are present to enable multi threaded SSL as documented here. Everything is working except on 2 web pages ilsole24ore.com ( seen in example down ), and...

Timer Interrupt for pthreads

How can I implement timer interrupt using pthreads? ...

Linux C++: Does a return from main() cause a multithreaded app to terminate?

This question seems like it's probably a duplicate, but I was unable to find one. If I missed a previous question, apologies. In Java, where I have most of my experience, if your main() forks a thread and immediately returns the process continues to run until all (non-daemon) threads in the process have stopped. In C++, this appears no...