Jump to content
Sign in to follow this  
flareback

pthreads barrier

Recommended Posts

working on a program in C. trying to use pthread_barrier_init(). the important parts to this question that I have so far is:

 

webStress.h

pthread_barrier_t *startLine;

main.c

#include "webStress.h"
#include <pthread.h>
...
if((pthread_barrier_init(startLine, NULL, 1)) == 0){
  printf("error\n");
  exit(0);
}

thread.c

#include "webStress.h"
#include <pthread.h>
...
pthread_barrier_wait(startLine);

The point of the barrier is I will have multiple threads running and I want them to all wait to begin at once. The code I have compiles fine, no errors, no warnings and I compile with the -lpthread flag. But when I run it, it segfaults in the init portion. The following is what gdb tells me.

Program received signal SIGSEGV, Segmentation fault.

[switching to Thread -1208600384 (LWP 961)]

0x002dab32 in pthread_barrier_init () from /lib/i686/nosegneg/libpthread.so.0

Any ideas? I can't think of anything. I know barrier isn't implemented on certain distros but I can't understand why it would compile if it wasn't on there. I've never used barriers before and don't know if i'm implementing wrong or what.

 

at the point when I get the seg fault, I haven't created any other threads, or called pthread_barrier_wait(). The init gets called directly after checking the command line args.

 

This is on a linux system by the way. don't know what distro, it's a school comp and my only way to get into it is ssh (unless someone can tell me how to check using ssh).

Share this post


Link to post
Share on other sites

I haven't used it either, but at a guess I would say it's because the pointer you're passing for parameter #1 (startLine) is a pointer with no allocated memory. It's pointing to a random location, and I expect the pthread functions are then writing to the memory at this random location.

 

Try these instead:

 

pthread_barrier_t startLine;

pthread_barrier_init(&startlLine, NULL, 1);

pthread_barrier_wait(&startLine);

 

or

 

pthread_barrier_t *startLine = NULL;

startLine = (pthread_barrier_t *)malloc(1 * sizeof(pthread_barrier_t));

/* possible 'memset' here */

pthread_barrier_init(startLine, NULL, 1);

pthread_barrier_wait(startLine);

 

 

 

This is on a linux system by the way. don't know what distro, it's a school comp and my only way to get into it is ssh (unless someone can tell me how to check using ssh).

 

`uname -a`

Share this post


Link to post
Share on other sites

thanks markiemrboo,

 

uname -a

[[email protected] jkcj]$ uname -a

Linux teamB1 2.6.17-1.2157_FC5xenU #1 SMP Wed Jul 12 00:46:43 EDT 2006 i686 athlon i386 GNU/Linux

is that Fedora Core 5? It is being run virtually through xen i know that much.

 

anyways, the first worked. Didn't have to malloc anything.

Share this post


Link to post
Share on other sites
thanks markiemrboo,

 

No problem,

 

uname -a

 

is that Fedora Core 5? It is being run virtually through xen i know that much.

 

Looks like it might be. Not much of a Linux guy as you all know, but FC5 seems to suggest it is :)

 

anyways, the first worked. Didn't have to malloc anything.

 

Either should work, just done in slightly different ways :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×