pthreads

VS rand() problem with pthread-win32

I come into a strange problem in pthread programming I've compiled the following code in vs2005 with pthread-w32 #include <cstdio> #include <cstdlib> #include <ctime> #include <pthread.h> #include <windows.h> pthread_mutex_t lock; void* thread1(void *) { int r1; while(true) { pthread_mutex_lock(&lock); // rand is maybe a CS ...

When is it safe to destroy a pthread barrier?

If I have an initialised pthread_barrier_t, when is it safe to destroy it? Is the following example safe? pthread_barrier_t barrier; ... int rc = pthread_barrier_wait(b); if (rc != PTHREAD_BARRIER_SERIAL_THREAD && rc != 0){ perror("pthread_barrier_wait"); exit(1); } if (id == 0){ if(pthread_barrier_destroy(&(threads[t_root].info...

gcc: Do I need -D_REENTRANT with pthreads?

On Linux (kernel 2.6.5) our build system calls gcc with -D_REENTRANT. Is this still required when using pthreads? How is it related to gcc -pthread option? I understand that I should use -pthread with pthreads, do I still need -D_REENTRANT? On a side note, is there any difference that you know off between the usage of REENTRANT betwee...

C++ - threads and multiple queues

I need to build a system of workers (represented as threads) and (multiple) queues. Individual jobs are waiting in one of the queues and waits for a worker thread to process them. Each worker can process jobs only from some of the queues. No spin-waiting. C/C++, pthreads, standard POSIX. The problem for me is the "multiple queues" thing...

Nice-Level for pthreads?

On an embedded system (Linux kernel 2.6.28 on ARM processor using glibc 2.6.1) I am running an application consisting of multiple threads. I would like one of those threads to get more CPU time than the others. One option for setting priorities seems to be to use pthread_setschedparam with SCHED_RR (or SCHED_FIFO), however this gives th...

Waiting on multiple events C++

Is there a recommended way to wait on multiple inputs. For example I would like my program to be able to receive input from 3 sources: Listen on a thread condition e.g. pthread_cond_wait() Take data from Standard input e.g. getline() Listen on a socket e.g. accept() What is the best way to accomplish this? Do I need a thread for each...

pthread-based event waking up only waiting threads

In my C++ program I have a class CEvent with trigger and wait member functions based on pthreads (running on Linux). Implementation is quite obvious (i.e. many examples online) if there is one waiting process. However now I need to satisfy the requirement that multiple threads are waiting on the event and should ALL wake up reliably when...

Using gprof with pthreads

Can gprof be used to profile a multi-threaded program that uses pthreads? That is, will its output include the time used in all the threads? ...

Event / Task Queue Multithreading C++

I would like to create a class whose methods can be called from multiple threads. but instead of executing the method in the thread from which it was called, it should perform them all in it's own thread. No result needs to be returned and It shouldn't block the calling thread. A first attempt Implementation I have included below. The ...

Parallelization: pthreads or OpenMP?

Most people in scientific computing use OpenMP as a quasi-standard when it comes to shared memory parallelization. Is there any reason (other than readability) to use OpenMP over pthreads? The latter seems more basic and I suspect it could be faster and easier to optimize. ...

pthreads compile but binary is not created.

I am trying to compile a program I wrote in C++ for an assignment that uses pthreads. I am using Eclipse in Linux, and I didn't have any problems compiling, after I added "-lpthread" to the compiler arguments (to g++, gcc and linker). However, when I was about to run and debug, Eclipse gave me an error message window "Launch failed. Bina...

Pthread - What is the difference between time.h::sleep() and pthread.h::pthread_yield()?

I spent a good long while looking for info on the differences between time.h::sleep() and pthread.h::pthread_yield() but was unable to find any solid reference material and so I am posting this question. What is the difference between time.h::sleep() and pthread.h::pthread_yield()? Update: The reason I ask is because I was using sleep...

segmentation fault on pthread_mutex_lock

hey, sorry to be a bother, but I need help urgently. I searched for this before I posted this question. I'm getting a segmentation fault when I try to do pthread_mutex_lock(&_mutex). This is really odd, I'm not sure what might have caused it. I have initialized _mutex in the constructor with pthread_mutex_init(&_mutex,NULL). anythi...

Using pthread_setspecific and pthread_getspecific to store a pointer to a std::map instance.

I'm using a map as a thread specific cache to keep track of failed LDAP searches. I dynamically allocate the map and store the pointer using pthread_setspecific. When checking the cache or incrementing the failure count I use pthred_getspecific in order to retrieve the void* pointer and static_cast the pointer back to my map type. Cal...

Need help on choosing locks for thread synchronization

I have several modifying threads and some reading threads, which all access the global variable X. I want to make my synchronization policy like this: When a thread try to modify X, it will require a lock first, and several modifying threads can have several locks required. When a thread try to read X, it must wait until all the modify...

C Programming: Debugging with pthreads

One of the hardest things for me to initially adjust to was my first intense experience programming with pthreads in C. I was used to knowing exactly what the next line of code to be run would be and most of my debugging techniques centered around that expectation. What are some good techniques to debugging with pthreads in C? You can s...

database transactions and threads

I have functions lock(), unlock(), query1(), query2() and query3(). The query functions just run some query on a database and can be considered r/w access to it. They do not lock. My system is multithreaded. The functionality I want is: If lock() is called from thread p1, only queries from thread p1 will run and queries from all other t...

Not locking mutex for pthread_cond_timedwait and pthread_cond_signal ( on Linux )

Is there any downside to calling pthread_cond_timedwait without taking a lock on the associated mutex first, and also not taking a mutex lock when calling pthread_cond_signal ? In my case there is really no condition to check, I want a behavior very similar to Java wait(long) and notify(). According to the documentation, there can be ...

wait fails in multithreaded application at 2.4 kernel

I have an application that is multithreaded - one thread is responsible for collecting the dead children with wait(), anther thread spawns them with fork upon request. I found out that on one platform with 2.4 kernel and LinuxThread wait always fails with ECHILD. I've found out that problem might be in non-POSIX compliant implementation...

A Unique and Constant Identifier for a pthreads thread?

I presumed that a pthread_t remains constant - for a given thread - for its entire life, but my experimentation seems to be proving this assumption false. If the id for a given thread does not remain constant across its life, how can I store a pthread_t so another thread can use pthread_join to block until the thread is finished? For ot...