Home > pthread create bus > pthread_create bus error

Pthread_create Bus Error

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Getting bus error 10 with pthreads up vote 3 down vote favorite My command line tool keeps throwing the bus error: 10 message. Xcode debugger shows EXC_BAD_ACCESS message and highlights the function call that creates the thread. Manual debugging shows that the execution flow breaks at random positions inside the thread flow. I tried another compiler (gcc), but it ended up the same. Disabling pthread_mutex_lock() and pthread_mutex_unlock() doesn't help. I wrote this small example that reproduces the error. #include #include #include typedef struct thread_args { pthread_mutex_t* mutex; } thread_args; void* test(void* t_args) { printf("Thread initiated\n"); thread_args* args = (thread_args* )t_args; printf("Args casted\n"); pthread_mutex_lock(args->mutex); printf("Mutex locked\n"); pthread_mutex_unlock(args->mutex); printf("Mutex unlocked\n"); pthread_exit(NULL); } int main() { pthread_mutex_t mutex1; pthread_mutex_init(&mutex1, NULL); thread_args args; args.mutex = &mutex1; pthread_t* thread; printf("Initiating a thread\n"); pthread_create(thread, NULL, test, &args); return(0); } c multithreading debugging pthreads share|improve this question edited Jun 23 '15 at 19:57 Sourav Ghosh 71.1k1174130 asked Jun 23 '15 at 19:38 Eli Korvigo 3,4612730 add a comment| 3 Answers 3 active oldest votes up vote 2 down vote accepted I think, in your case, pthread_create(thread, NULL, test, &args); at this call, thread is a pointer and not allocated memory. So, essentially pthread_create() tries to write into uninitialized memory, which creates undefined behavior. Referring the man page of pthread_create() Before returning, a successful call to pthread_create() stores the ID of the new thread in the

here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Bus error on OSX - pthreads up vote 2 down vote favorite am http://stackoverflow.com/questions/31011959/getting-bus-error-10-with-pthreads trying to get my head around the following: Have a small program am trying to port to OSX(intel) which calls function doWork() via pthread_create, in the function, I start by creating an array of long like such: long myarray[DIMENSION] on OSX, for the following values of DIMENSION, I get the following: 0->65434 = fine 65435->67037 = SIGBUS 67037+ = SIGSEGV I'm totally confused here, I understand that SIGBUS is due http://stackoverflow.com/questions/3329379/bus-error-on-osx-pthreads to memory alignment issues usually, I checked sizeof(long) and it appears to be 8 on this platform. Can somebody point me in the right direction of docs I should be reading here? Here is the source: #include pthread.h #include stdio.h #define NUM_THREADS 5 #define DIMENSION 12345 void *doStuff(void *threadid) { long array[DIMENSION]; pthread_exit(NULL); } int main (int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0; t lt NUM_THREADS; t++){ printf("In main: creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL, doStuff, (void *)t); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); } c osx pthreads share|improve this question asked Jul 25 '10 at 13:40 lochii 515 add a comment| 1 Answer 1 active oldest votes up vote 6 down vote accepted It would appear you're overflowing the stack. You'll need to either turn the long array into a malloced one, or use pthread_attr_setstacksize and friends to create a larger stack when you call pthread_create. Default thread stack sizes vary a lot before platforms, which would explain why the code works on other platforms. https://computing.llnl.gov/tutorials/pthreads/#Stack has some example code. As to why you get a sigbus, it's probably because the act of creating the array is overwriting some part of pthreads

a Question Ask for Help Receive Real-Time Help Create a Freelance Project Hire for a Full Time Job Ways to Get Help Ask a Question Ask for Help Receive Real-Time Help Create a Freelance Project Hire for a Full Time Job Ways to Get Help Expand Search https://www.experts-exchange.com/questions/20994492/Bus-error-with-pthread.html Submit Close Search Login Join Today Products BackProducts Gigs Live Courses Vendor Services Groups Careers Store Headlines Website Testing Experts Exchange > Questions > Bus error with pthread Want to Advertise Here? Solved Bus error with pthread Posted on 2004-05-19 C 1 Verified Solution 4 Comments 802 Views Last Modified: 2011-09-20 Hi, This program takes files name as arguments and count lines on each by thread. in execution times it gave me "Bus error" #include pthread_create bus #include #include FILE *in; int count; int sum1=0,i; char Buffer[256]; CountLines (char *file){ strcpy(input, file); in = fopen(input, "r"); if (in == NULL) { printf("Error In Files opening\n"); exit(-1); } count = 0; while (1) { fgets (Buffer, 256, in); if (feof (in)) break; ++count; } /*sum1=sum1+count;*/ sum1=sum1+count; } int main (int argc, char *args[]) { pthread_t tid1; for (i=1;i

 

Related content

No related pages.