Like Tree4Likes
  • 1 Post By rags_to_riches
  • 1 Post By oogabooga
  • 1 Post By Elkvis
  • 1 Post By CornedBee

pthread_create() issue with argument 4

This is a discussion on pthread_create() issue with argument 4 within the C++ Programming forums, part of the General Programming Boards category; in the function pthread_create(), I want to pass a structure in the fourth argument place. like Code: struct message{ int ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    13

    pthread_create() issue with argument 4

    in the function pthread_create(), I want to pass a structure in the fourth argument place.

    like
    Code:
    struct message{
    
    int a;
    int b;
    };
    int main(){
    
    pthread thread1;
    message message1;
    message1.a = 4;
    message1.b = 5;
    
    return 0;
    }
    
    pthread_create(thread1, NULL, function, message1);
    
    void function(message message1){
    cout<<"A: "<<message1.a<<endl;
    }
    I cannot do that because the fourth argument should be (void*) ... please tell how can I pass the structure into that function.

    thanks
    Last edited by rehman12390; 03-25-2012 at 09:51 AM.

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    I am now able to pass a struct message type variable, but I have another problem....

    I am passing a dynamically created memory like.
    Code:
    int main(){
    message *message1 = new message;
    pthread thread1;
    
    message1.a = 4;
    message1.b = 5;
    
    
    pthread_create(&thread_new_connection, NULL, new_connection, message1));
    return 0;
    }
    void* new_connection(void* message1){
        
        delete message1; // problem is here
        pthread_exit(NULL); 
    }
    there is coming a warning, when I delete the dynamically allocated memory, it gives the warning i.e. deleting void* is undefined

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    A void * points into memory of unknown size because it has no type. In order to perform a delete (or do anything, really) the compiler must know the type to which a pointer is pointing.
    rehman12390 likes this.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Do something like this as your first statement in new_connection, otherwise you won't be able to do anything with the message since its type is unknown:

    message* mess = message*(message1);
    rehman12390 likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    Out of curiosity, what are the planes regarding your code/program?

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Guys, I have got a new problem with it. I am not able to delete the allocated memory, here lies the code

    Code:
    #include <iostream>
    #include <pthread.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h> //memset
    #include <stdlib.h>    //exit()
    #include <net/ethernet.h>
    #include <netinet/ip.h>    //Provides declarations for ip header
    #include <netinet/tcp.h>    //Provides declarations for tcp header
    #include <arpa/inet.h>
    
    using namespace std;
    
    struct message{
        int a;
        int b;
        char c[50];
    };
    pthread_mutex_t thread_lock;
    void* function(void*);
    
    int main(){
        
        int i=0;pthread_t thread;message *message1;
        while(1){
            while(i<50000){
                
                message1=new message;
                message1->a = i*1;
                message1->b = i*2;
                pthread_create(&thread, NULL, function, message1);
                i++;
            }
            i=0;
            sleep(5);
        }
        return 0;
    }
    
    void* function(void* data){
        
        sleep(3);
        message *message1 = (message*)data;
        pthread_mutex_lock(&thread_lock);
        cout<<"Result of Addition: "<<message1->a+message1->b<<endl;
        pthread_mutex_unlock(&thread_lock);
        //delete (message*)data
        //delete (message*)*&data;
        //delete message1;
        pthread_exit(NULL);
    }
    The problem is that I am not able to free the memory that was allocated in main with these three delete statements. consequently, the program fails, I think memory out when the memory usage reaches around 10.4%. Please tell how can I free the memory

  7. #7
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Shouldn't you be deleting the message1 only since that is the pointer to the data in the first place?


    delete (message*)data;
    delete (message*)*&data;
    Both those lines do the same thing so you are trying to delete memory that has already been deleted, and you are still using the void* data.

    delete message1;
    Keep it?
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Why the heck are you using pthreads instead of Boost or TBB is what I want to know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    The problem is not like that .........

    I know the
    Code:
    delete message1;
    works fine.

    but the problem is that the memory allocated in the main cannot be released by the threads, consequently after some time, the programs fails (crashes).

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Again, why are you using pthreads?

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    Again, why are you using pthreads?
    Why not? If the platform is pthread based, that's what Boost and TBB are going to use anyway. Nothing wrong with that, but there is also nothing wrong with learning the lower level base. Then if you want you can create your own wrappers and be spared the headache of learning someone else's higher level implementation. Methinks, glancing at TBB, that you are never going to learn how threading works that way.

    @rehman12390: I'm not so sure what you think is wrong with that code is actually what is wrong.

    A) Why do you believe "that I am not able to free the memory that was allocated in main with these three delete statements"?

    B) Why are you using an infinite loop? If you want to wait for the threads to exit, that is not the way to do it.

    C) Why are you experimenting with like 50,000 threads? That is a large part of what is eating your memory, and it is not just the memory allocated to each message. Each of those has additional stack overhead, probably a lot more than 1 message; the reason your program crashes is because you have exceeded your stack space, which has nothing to do with the messages on the heap. You are freeing all that heap memory allocated in main with the delete, but that is not the problem. This is the same thing that will happen with excessive recursion. If you think you need 50,000 threads for some purpose, you are wrong. There is no purpose to 50,000 threads. There is no purpose to 5000, or 500, or, generally, even 50 threads. Something about your design is very wrong. No one writes programs with hundreds or thousands of threads.
    Last edited by MK27; 03-27-2012 at 07:24 AM.
    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

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,370
    Quote Originally Posted by MK27 View Post
    No one writes programs with hundreds or thousands of threads.
    unless you're running on a cray
    MK27 likes this.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    Quote Originally Posted by MK27 View Post
    Why not? If the platform is pthread based, that's what Boost and TBB are going to use anyway. Nothing wrong with that, but there is also nothing wrong with learning the lower level base. Then if you want you can create your own wrappers and be spared the headache of learning someone else's higher level implementation. Methinks, glancing at TBB, that you are never going to learn how threading works that way.
    Do you seriously fail to understand the principle of abstraction?
    Why do we not use pthread? Because it's lower level than boost and TBB. Hence, because it is lower level, it is less abstracted.
    Less abstraction and lower level code leads to messier code that is harder to understand, use and maintain. Also creates more bugs!

    Sure, they may not understand how threading works, but so what? Unless you really need to work at a threading level, you do not need to know, and that is the principle of abstraction. You don't need to know how the lower layer works. You only need to know how the interface works.

    If you need to implement your own threading library, then yes, you need to know how threading works. But the OP isn't doing that, is he/she?
    And even when working with lower level stuff, you always abstract it to work with higher level primitives for reasons outlined above.

    If you want to use pthreads, use C. Otherwise you may as well throw away all the advantages a higher level such as C++ provides.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Posts
    13
    Hi there ... after a long time, I was little busy, again with the same problem.

    Problem: My program is eating memory and consequently it crashes. Why the program crashes ?

    I am testing the program for 50,000 threads, that amount of thread may not look fine but the program should work fine. Each thread after doing its work must terminate and that memory should stay constant.

    Actually, I want to write a server program that need to tackle with large amount of connection at a single time. Therefore, the number of threads at a time may reach thousands. Can you please elaborate why the program continues to eat more and more memory or in simple why the memory is not staying at constant level ?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    50000 threads at the same time probably just won't work. Serially, they should, if you clean up properly. (Not sure if pthreads requires any explicit cleanup.) Which is it?

    Anyway, thread-per-connection is not necessarily a good way to write a server. Using async I/O and a fixed number of threads (typically same as the number of cores) to service requests is a more performant approach. See the Boost.Asio library.
    phantomotap likes this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Argument inside the Argument of a Function Declaration
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 06-11-2011, 05:53 AM
  2. Issue with "double" argument type
    By seanksg in forum C Programming
    Replies: 10
    Last Post: 03-25-2011, 03:22 AM
  3. pthread_create tid?
    By micke_b in forum C Programming
    Replies: 5
    Last Post: 10-24-2007, 07:54 AM
  4. pthread_create() Problem..
    By anwar_pat in forum C Programming
    Replies: 2
    Last Post: 09-21-2006, 01:11 AM
  5. About pthread_create
    By Lau in forum C Programming
    Replies: 7
    Last Post: 11-17-2003, 10:10 AM

Tags for this Thread


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