-
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?
-
> 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.