Thread: posix thread help

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    posix thread help

    I am working on a producer/consumer problem and need a little help on calling my functions in a posix thread. What would be the correct syntax for calling producer?

    Nothing seems to be working for me today. I have had to delete all my curly braces from from my code in order for it to let me post. When I left them in it kept telling me to use code tags, which I was already using.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cctype>
    #include <string>
    #include <pthread.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    using namespace std;
    
    #define N 100
    typedef int semaphore;
    semaphore mutex =1;
    semaphore empty = N;
    semaphore full = 0;
    bool TRUE = TRUE;
    pthread_t thread1, thread2, thread3, thread4;
    int err1, err2, err3, err4;
    	
    void producer();
    int remove_item();
    void consumer();
    int produce_item();
    void insert_item (int item);
    semaphore down (semaphore value);
    int up (int &value);
    void consume_item(int item);
    
    
    int main(int argc, char* argv)
    // 
    err1 = pthread_create(&thread1, NULL, producer() (void*));//trying to call producer here
    //	
    
    void producer()
    //
    	cout<<("producer stuff")<<endl;
    	int item;
    	
    	while (TRUE)
    	//
    		item = produce_item();
    		down(empty);
    		down(mutex);
    		insert_item(item);
    		up(mutex);
    		up(full);
    	//
    
    //
    An explanation of parts of the thread call might help
    I.E
    pthread_create(&thread1, NULL, producer() (void*));
    1st slot:
    &thread = the pthread you want to use?
    2nd slot:
    NULL= something?
    3rd slot:
    Produce() = the fuction i am trying to call?
    4th slot:
    (void*) = some crazy C thing?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > I have had to delete all my curly braces from from my code in order for it to let me post.
    NO!
    That's to encourage you to use code tags - which you seem to have done.
    Curly brackets outside of [code][/code] tags are disallowed.

    > void producer();
    A thread function should be declared
    void *producer( void *);

    Then the call to create a thread is
    pthread_create(&thread1, NULL, producer, NULL) ;

    The 4th parameter to pthread_create() gets passed as the first parameter to producer() when it eventually gets called as a thread. Use this when you have some information you want to pass to your thread.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 07:27 AM
  2. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  3. Replies: 2
    Last Post: 07-01-2007, 07:11 AM
  4. Messaging Between Threads, Exiting Thread On Demand, Etc.
    By HyperShadow in forum C++ Programming
    Replies: 10
    Last Post: 06-09-2007, 01:04 PM
  5. Multi-Thread Programming
    By drdroid in forum C++ Programming
    Replies: 6
    Last Post: 04-04-2002, 02:53 PM