0

I'm working on a project where I need to make a program run on multiple threads. However, I'm running into a bit of an issue.

In my program, I have an accessory function called 'func_call'. If I use this in my code:

func_call((void*) &my_pixels);

The program runs fine.

However, if I try to create a thread, and then run the function on that, the program runs into a segmentation fault.

pthread_t thread;
pthread_create (&thread, NULL, (void*)&func_call, (void*) &my_pixels);

I've included pthread.h in my program. Any ideas what might be wrong?

5
  • a bit hard to tell from the small fragment given here but is the my_pixels argument going out of scope and then the memory it took up is getting overwritten with some other values leading to random pointer values
    – Nick
    Mar 11, 2012 at 1:06
  • my_pixels is an instance of a struct with the variables that I need to send the function. I initialize the struct and its variables in the main function before calling on the pthread. Once the program enters the func_call function, it saves the variables locally. Mar 11, 2012 at 1:09
  • @user: what's the lifetime of my_pixels? How does func_call use the thread parameter? Is there any synchronization between the main thread and func_call? Please show the code; an explanation won't provide all relevant details. Mar 11, 2012 at 1:18
  • Have you used a debugger, such as gdb, to see where the error is happening? It may give you an idea what to look for, as you must be referencing some point incorrectly. Mar 11, 2012 at 1:21
  • The more I think about it, the more I'm convinced that it's a reference error. I just can't figure out where, and why the error doesn't come up when I run the function on its own. Mar 11, 2012 at 1:30

3 Answers 3

0

You are not handling data in a thread safe manner:

  • the thread copies data from the thread argument, which is a pointer to the main thread's my_pixels variable; the main thread may exit, making my_pixles invalid.
  • the thread uses scene, main thread calls free_scene() on it, which I imagine makes it invalid
  • the thread calls printf(), the main thread closes stdout (kind of unusual itself)
  • the thread updates the picture array, the main thread accesses picture to output data from it

It looks like you should just wait for the thread to finish its work after creating it - call pthread_join() to do that.

For a single thread, that would seem to be pointless (you've just turned a multi-threaded program into a single threaded program). But on the basis of code that's commented out, it looks like you're planning to start up several threads that work on chunks of the data. So, when you get to the point of trying that again, make sure you join all the threads you start. As long as the threads don't modify the same data, it'll work. Note that you'll need to use separate my_pixels instances for each thread (make an array of them, just like you did with pthreads), or some threads will likely get parameters that are intended for a different thread.

1
  • That did it! Thanks a bunch! So to join the threads, a simple for loop would suffice, yes? Mar 11, 2012 at 2:37
0

Without knowing what func_call does, it is difficult to give you an answer. Nevertheless, here are few possibilities

  1. Does func_call use some sort of a global state - check if that is initialized properly from within the thread. The order of execution of threads is not always the same for every execution
  2. Not knowing your operating system (AIX /Linux/Solaris etc) it is difficult to answer this, but please check your compilation options
  3. Please provide the signal trapped and atleast a few lines of the stack-trace - for all the threads. One thing you can check for yourself is to print the threads' stack-track (using threads/thread or pthread and thread current <x> based on the debugger) and and if there is a common data that is being accessed. It is most likely that the segfault occurred when two threads were trying to read off the other's (uncommitted) change

Hope that helps.

Edit: After checking your code, I think the problem is the global picture array. You seem to be modifying that in the thread function without any guards. You loop using px and py and all the threads will have the same px and py and will try to write into the picture array at the same time. Please try to modify your code to prevent multiple threads from stepping on each other's data modifications.

4
  • Thanks for the reply. I'm pretty sure func_call is initialized correctly. It runs perfectly fine as a standalone function. I'm coding this in C, but running it on a Linux terminal. Also, I'm currently trying to get this code to work on only one thread, before moving on to multiple threads. So, I think the segfault must come up from something else. Mar 11, 2012 at 1:15
  • Did the stack-trace of the core file help in narrowing down the possibilities of the cause of the issue. Like @Nick mentioned, it is possible that some pointers in your are data could be corrupt leading to the segfault
    – Gangadhar
    Mar 11, 2012 at 1:19
  • The backtracing isn't helping. I'm convinced it's one of my pointers is to blame, so I'm trying to debug with a few different breakpoints. Mar 11, 2012 at 1:47
  • I assume you are talking about modifying the global data by a single thread. And if so, no, it won't be a problem.
    – Gangadhar
    Mar 11, 2012 at 2:41
0

Is func_call a function, or a function pointer? If it's a function pointer, there is your problem: you took the address of a function pointer and then cast it.

People are guessing because you've provided only a fraction of the program, which mentions names like func_call with no declaration in scope.

Your compiler must be giving you diagnostics about this program, because you're passing a (void *) expression to a function pointer parameter.

Define your thread function in a way that is compatible with pthread_create, and then just call it without any casts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.