Thread: Parsing Struct Objects to Threads

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Parsing Struct Objects to Threads

    Hello friends. I'm just wondering what the correct way to parse a structure object in a looped CreateThread would be. I'll explain what I mean.

    Basically, I have a structure object, and I want each newly created thread to have a copy of this structure object. The way I'm doing it at the moment is like so (parsing a pointer to the structure and converting it to a struct object from a pointer to void):

    Code:
    my_struct x;
    
    for (unsigned int i = 0; i < scan_args.threads; i++) {
            x.thread_id = i;
    	CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&Initialize_Thread, &x, 0, 0);
    }
    
    Initialize_Thread(void * x) {
            my_struct myStructure = *(my_struct *)x;
            // myStructure is now a copy of the original structure
    }
    The problem with this, it's causing a race condition because the for loop is executing faster than each thread is copying structures. Therefore, the copied structures member (thread_id) is usually wrong. This is because at the point at which the thread copies the structure the original structures thread_id has already changed multiple times (aka the for loop is steps ahead of the thread).

    So, how exactly can I copy the structure inside the for loop instead of parsing a pointer, and instead just parse the copy.

    If I was to do it like this:

    Code:
    for (unsigned int i = 0; i < scan_args.threads; i++) {
            x.thread_id = i;
            my_struct copy = x;
    	CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&Initialize_Thread, &copy, 0, 0);
    }
    Wouldn't this cause race conditions as well? Because... Is the memory free'd or overwritten or manipulated each time through the for loop, or once the scope of the loop has been broken?

    I hope my explanation wasn't too confusing... Thanks!
    Last edited by pobri19; 04-18-2009 at 05:09 AM.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Threads are evil. You never know when which one is exevuting. If you need them, use semaphores or mutexes, but they can slow things down quite a lot. Look at some explanations.

    To your question: might be, might not be. If the compiler is intelligent enough and optimizing, it will use the same space for each copy object. And if the loop is too fast, it will overwrite the old one while your thread is reading it. But some compilers (not many) allocate new space every time. Then it's likely that they'll unlink your old copy. As I said, try to avoid threads. They're not fun.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by Brafil View Post
    Threads are evil. You never know when which one is exevuting. If you need them, use semaphores or mutexes, but they can slow things down quite a lot. Look at some explanations.

    To your question: might be, might not be. If the compiler is intelligent enough and optimizing, it will use the same space for each copy object. And if the loop is too fast, it will overwrite the old one while your thread is reading it. But some compilers (not many) allocate new space every time. Then it's likely that they'll unlink your old copy. As I said, try to avoid threads. They're not fun.
    Well, since each thread will be handling a socket connection, I think it's my only viable solution. I don't see how semaphores or mutexes would be able to accomplish the same.

    Any who, the rest of my program works fine. I don't need to know in what order the threads are executing... It's not important. And... I want to get around the fact that the loop is going to overwrite my old structure, hence why I made this thread, and why I want to use copies instead of a pointer to the original object.

    I also want these copies not to be overwritten or free'd in memory whilst my program is running (until maybe a little bit later when I can have another loop for freeing the memory).

    But yeah, this is why I'm asking. I'm uncertain how to do this.
    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Brafil View Post
    Threads are evil. You never know when which one is exevuting. If you need them, use semaphores or mutexes, but they can slow things down quite a lot. Look at some explanations.

    To your question: might be, might not be. If the compiler is intelligent enough and optimizing, it will use the same space for each copy object. And if the loop is too fast, it will overwrite the old one while your thread is reading it. But some compilers (not many) allocate new space every time. Then it's likely that they'll unlink your old copy. As I said, try to avoid threads. They're not fun.
    Wow. Just because you fail to grasp a subject doesn't make it evil right away. Threads are, when used properly, dead-useful.

    In this case, I'd say, just create a new object (using malloc, for instance) of the instance, pass a pointer to the thread, and let the thread delete it again. No need for mutexes here.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Wow. Sorry. Maybe I was a bit too fatuous. Or I'm just not able to handle them properly.
    Last edited by Brafil; 04-18-2009 at 06:29 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  3. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM