creating POSIX threads in UNIX

This is a discussion on creating POSIX threads in UNIX within the C++ Programming forums, part of the General Programming Boards category; Code: #include <pthread.h> #include <iostream> using namespace std; void myfunc() { cout<<"hello"<<endl; } int main() { pthread_t *thread1; thread1 = ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    26

    creating POSIX threads in UNIX

    Code:
    #include <pthread.h>
    #include <iostream>
    using namespace std;
    
    
    void myfunc()
    {
            cout<<"hello"<<endl;
    }
    int main()
    {
    
            pthread_t *thread1;
            thread1 = (pthread_t*)calloc(1,sizeof(pthread_t));
            pthread_create(thread1,NULL,(void *)(&myfunc),NULL);
            return 0;
    }
    when i try to run get an error saying: main.cpp:15: error: invalid conversion from `void*' to `void*(*)(void*). This is just to test to see that i can create a thread to simualte a producer/consumer threads.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Give this a try:

    Code:
    #include <pthread.h>
    #include <iostream>
    using namespace std;
    
    
    void* myfunc(void *param)
    {
            cout<<"hello"<<endl;
    }
    int main()
    {
    
            pthread_t *thread1;
            thread1 = (pthread_t*)calloc(1,sizeof(pthread_t));
            pthread_create(thread1,NULL,myfunc,NULL);
            return 0;
    }

  3. #3
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Code:
      #include <pthread.h>
      #include <iostream>
      using namespace std;
      
      void *myfunc(void *data)
      {
      cout<<"hello"<<endl;
      }
      int main()
      {
      
              pthread_t *thread1;
              thread1 = (pthread_t*)calloc(1,sizeof(pthread_t));
              pthread_create(thread1, NULL, myfunc, NULL);
              return 0;
      }
    (edit) next time I'll leave the colors away to be faster°°

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    when i execute that code i get a: Segmentation fault (core dumped)

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You get a seg fault because your main thread ends before your spawned thread.
    Give this a try:

    Code:
    void* myfunc(void *param)
    {
       cout<<"hello"<<endl;
       return 0;
    }
    int main()
    {
       pthread_t thread1;
       pthread_create(&thread1,NULL,myfunc,NULL);
       pthread_join(thread1,0);
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    The pthread_join() function tells the main thread to wait for the spawned thread to end before continuing.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    26
    i tried doing that, now it gives me a /var/tmp//ccjC6FRg.o(.text+0x62): In function `main':
    : undefined reference to `pthread_join'

    I have #include<pthread.h>

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You have to link to the pthread library. Pass -lpthread to your linker.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 11:28 AM
  2. POSIX threads
    By Boylett in forum Linux Programming
    Replies: 2
    Last Post: 09-10-2008, 02:33 PM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 10:38 AM
  4. POSIX threads policy and priority
    By arunj in forum Linux Programming
    Replies: 2
    Last Post: 06-13-2006, 04:48 AM
  5. compile prob: sched.h (win32 POSIX threads) - pid_t
    By BrianK in forum Windows Programming
    Replies: 6
    Last Post: 04-11-2003, 06:52 PM

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