Do I need to use semaphores for this problem?

This is a discussion on Do I need to use semaphores for this problem? within the C Programming forums, part of the General Programming Boards category; I have to modify an image filtering program to divide the work among separate threads. There will be two image ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    Do I need to use semaphores for this problem?

    I have to modify an image filtering program to divide the work among separate threads. There will be two image pointers (just a redefinition of a unsigned char pointer). One is the image to be modified, and the other is the output pointer. Each thread will only read values from the input image, which will not be modified at all. And each thread will write indepedent pieces of the output image. So, is there any reason to use semaphores? I've never used threads before, but I don't see the necessity for them in this program, since I can't think of any situation for corruption/race conditions.

    Second question; where do variables have to be declared to be part of the shared memory across all threads? Will variables made inside of main be shared, or do they have be declared globally before main? If the second situation is the case, would I be fine just passing the image pointer values in a struct to each thread?

    Thanks for the help guys!

    edit: For the shared memory issue, If i just declare static image pointers outside of main, then assign them before initiating threads, the threads can then access those pointers, right?
    Last edited by Voondebah; 04-20-2009 at 02:48 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If neither thread modifies the shared data, then there is no need for synchronization.

    Second question; where do variables have to be declared to be part of the shared memory across all threads?
    Any variable that is not local to a function will be shared. So you could declare the variable globally, or use malloc() to allocate memory for the object and pass a pointer to that memory to multiple threads.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Problem with POSIX semaphores
    By kaseo88 in forum C Programming
    Replies: 7
    Last Post: 06-29-2007, 12:31 PM
  4. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21