Like Tree1Likes
  • 1 Post By Salem

[C] Thread in Thread in thread {multithread}

This is a discussion on [C] Thread in Thread in thread {multithread} within the C Programming forums, part of the General Programming Boards category; What's the difference as staters? _beginthread(ServerTalking,0, (void *)sockfd); en _beginthread(ServerTalking,0, &sockfd);...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    19

    [C] Thread in Thread in thread {multithread}

    What's the difference as staters?

    _beginthread(ServerTalking,0, (void *)sockfd);

    en

    _beginthread(ServerTalking,0, &sockfd);

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    &sockfd means Address of sockfd, and (void *)sockfd means sockfd.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    BillyTKid is correct, but note that since "sockfd" is just a label, these two calls might be identical. I'm guessing that they are, and that the final parameter here (a la pthreads) is a void*. So:

    Code:
    int sockfd; // will work with &sockfd
    int *sockfd; // will work with (void*)sockfd;
    Actually, both of those will work with either call but accomplish different ends. Also, many people would probably consider casting the first one as a pointer bad style.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > Also, many people would probably consider casting the first one as a pointer bad style.
    Perhaps, but it's probably the lesser of two evils.

    > _beginthread(ServerTalking,0, &sockfd);
    The problem here is all sorts of thread synchronisation issues. Two threads now have a pointer to the same memory location.
    The parent thread cannot let that variable go out of scope, or modify it, until it is sure the child thread has finished using it.

    There have been several examples of this kind of sorry code posted before.
    Code:
    void myStarter ( int sockfd ) {
        _beginthread(ServerTalking,0, &sockfd); // don't do this!
    }
    It's like returning a pointer to a local variable, only much harder to track down!

    Ideally, you should do this.
    Code:
    struct info {
      int sockfd;
    };
    void myStarter ( int sockfd ) {
        struct info *p = malloc( sizeof *p );
        if ( p ) {
            p->sockfd = sockfd;
            _beginthread(ServerTalking,0, p); 
            // p now belongs to the thread, so it will free it
            // when it has finished using it.
        }
    }
    The parent thread doesn't have to worry any more about keeping shared data safe, because there isn't any.
    rags_to_riches likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by Salem View Post
    > Also, many people would probably consider casting the first one as a pointer bad style.
    Perhaps, but it's probably the lesser of two evils.

    > _beginthread(ServerTalking,0, &sockfd);
    The problem here is all sorts of thread synchronisation issues. Two threads now have a pointer to the same memory location.
    The parent thread cannot let that variable go out of scope, or modify it, until it is sure the child thread has finished using it.

    There have been several examples of this kind of sorry code posted before.
    Code:
    void myStarter ( int sockfd ) {
        _beginthread(ServerTalking,0, &sockfd); // don't do this!
    }
    It's like returning a pointer to a local variable, only much harder to track down!

    Ideally, you should do this.
    Code:
    struct info {
      int sockfd;
    };
    void myStarter ( int sockfd ) {
        struct info *p = malloc( sizeof *p );
        if ( p ) {
            p->sockfd = sockfd;
            _beginthread(ServerTalking,0, p); 
            // p now belongs to the thread, so it will free it
            // when it has finished using it.
        }
    }
    The parent thread doesn't have to worry any more about keeping shared data safe, because there isn't any.

    Thanks for the info! I will apply that code later for now I have another problem:
    I don't know why but it doesn't want to work when I put fgets there, I need something that constantly is reading my typing to check it later for lines like /dcc /msg. (This is just simplified code I made)
    And I can't figure out why the fgets can be a problem.

    Code:
    #include <windows.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include <conio.h>
    #include <process.h>
    
    // Secound Thread function
    void ThreadProc(void *param);
    void ThreadProc2(void *param);
    
    // First thread 
    int main()
    {
        int val = 0;
    
        printf("Thread Demo\n");
        while(1){
        _beginthread( ThreadProc,0,(void *)val); // create thread
        }
        //WaitForSingleObject(handle,INFINITE);
        return 0;
    }
    
    
    void ThreadProc(void *param)
    {
        //HANDLE handle;
        int val = 0;
        int h= (int)param;
     
        char inputText[300];
        printf("Thread 1\n");
        
        //Problem here
        fgets(inputText, sizeof inputText, stdin);
    
        _beginthread(ThreadProc2,0,(void*)h); // create thread
    
        //_endthread();
    
    }
    
    void ThreadProc2(void *param)
    {
        /*while(1) {
            printf("Thread 2\n");
        
        
        _endthread();*/
    
    
        printf("Thread 2\n");
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > while(1)
    How about 1 thread, instead of 1000's all trying to read stdin at the same time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    19
    Quote Originally Posted by Salem View Post
    > while(1)
    How about 1 thread, instead of 1000's all trying to read stdin at the same time.
    As I said before this is simplified code In the real code ThreadProc2 is a listen socket that is constantly listening for incoming connections.

    ThreadProc in the real code is a irc client that is constantly check for packets from the server and checking if the user types commands.

    The stdin is needed to read / check for input commands

    It looks like this for a part:

    Code:
    fgets(inputText, sizeof inputText, stdin);
    
    
        //break if quit;
        if(strstr(inputText, "quit") || strstr(inputText, "Quit") || strstr(inputText, "QUIT") || strstr(inputText, "QUit") || strstr(inputText, "QUIt") || strstr(inputText, "qUIt") || strstr(inputText, "quIt"))
        {
            sendToServer(inputText,(void *)sockfd);
            exit(0);
        }
        else if(strstr(inputText,"/dcc"))
        {
    
            //char *user, *port;
            //string filename;
            //DCC SEND <filename> <ipaddress> <port> <filesize>
            
        /*    strInputText = inputText;
            strInputText = strInputText.replace (0,1,"/dcc");
            
            printf(" #### %s",strInputText.c_str());*/
            //printf("Output: %s",filename);
            
            int randNumer=10;
    
             
            printf("\n\nThread1\n\n");
            _beginthread(ThreadProc2,0, (void *)numbytes);
            
        }
        else
        {
            sendToServer(inputText,(void *)sockfd);
        }
    Last edited by Vincent Wouters; 11-06-2011 at 02:49 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > if(strstr(inputText, "quit") || strstr(inputText, "Quit") || strstr(inputText, "QUIT") || strstr(inputText, "QUit") || strstr(inputText, "QUIt") || strstr(inputText, "qUIt") || strstr(inputText, "quIt"))
    Do you have a locally defined strcasecmp(), or perhaps write your own to say lowercase the input string and then do one comparision.
    Then you don't miss things like "QuIt" and the other half dozen or so permutations.

    > As I said before this is simplified code In the real code ThreadProc2 is a listen socket that is constantly listening for incoming connections.
    > ThreadProc in the real code is a irc client that is constantly check for packets from the server and checking if the user types commands.
    I would suggest you get something working well for a single connection only.
    Multiple connections would probably mean you creating a separate "window" with some idea of "focus" associated with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-09-2010, 07:31 PM
  2. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  3. Message box as thread to stop main thread
    By Yasir_Malik in forum Windows Programming
    Replies: 8
    Last Post: 04-11-2006, 11:07 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM

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