So I have a program which is supposed to have consumer and producer threads.
In the main method, I want to call init_consumers() which will call ptread_init()
Here is some code:
int init_consumers(char *quantity, pthread_t **cons, void *complex_obj)
{
//seting the limit acording to "quantity"
for (; i < limit; i++)
pthread_create(cons[i], NULL, &consumer, &complex_obj);
return(i);
}
the main:
#include <pthread.h>
#define MAX_PROD 50
#define MAX_CONS 50
///main entry point of the program
int main(int argc, char *argv[])
{
pthread_t prod[MAX_PROD];
pthread_t cons[MAX_CONS];
struct complex_obj *co;
//some code
co = create_cplx_obj();
//complex object manipulation code
init_producers(argv[2], &prod, co);
init_consumers(argv[3], &cons, co);
//more code
exit(0);
}
argv[2] and argv[3] is how many producers/consumers the user wants.
Also, the thread signature is: void *producer(void *args);
I still get problems with the * and &, so my problem is when making the method call and signature.
The error I was getting was
n_prod_n_cons.c:158:6: note: expected ‘pthread_t * {aka long unsigned int *}’ but argument is of type ‘pthread_t (*)[50] {aka long unsigned int (*)[50]}’ int init_producers(char *quantity, pthread_t *prod, void *complex_obj)
As indicated (tersely) in my comment, you have arrays of pthread_t in main(); pass them to the launchers just as you would an array of int. Make the signature of the launcher functions match what you'd do if you passed an array of int. Then use &array[index] to pass a pointer to a single row of the array to pthread_create(). That adds up to:
In main():
init_producers(argv[2], prod, co);
init_consumers(argv[3], cons, co);
(Your code was passing a pthread_t (*)[MAX_PROD] — pointer to a fixed size array of pthread_t — to the launcher functions, quite different from the signatures which expected a pthread_t **.)
The launchers look like:
int init_consumers(char *quantity, pthread_t *cons, void *complex_obj)
{
// setting the limit acording to "quantity"
for (; i < limit; i++)
pthread_create(&cons[i], NULL, consumer, &complex_obj);
return(i);
}
cons[i] is the ith entry in the array of pthread_t; passing its address give the pthread_create() function the pthread_t * it expects.
Related
I am trying to create dynamic number of thread.....
#include<stdio.h>
#include<pthread.h>
void* thread_function(void)
{
printf("hello");
}
int main(int argc,char *argv[])
{
int noOfThread= atoi(argv[1]);
pthread_t thread_id[noOfThread];
int i;
int status;
for(i=0;i<noOfThread;i++)
{
pthread_create (&thread_id[i], NULL , &thread_function, NULL);
}
for(i=0;i<noOfThread;i++)
pthread_join(thread_id[i],NULL);
}
3 errors:
implicit declaration of function atoi....
passing arguement 3 of 'pthread_create' from incompatible pointer type
expected 'void * (*)(void *)' but arguement is of typr'void * (*) (void)'......
There are several issues here:
You need to include stdlib.h for the declaration of atoi()
A pthread task function has a void* argument. This will fix issue #2 and #3.
(http://man7.org/linux/man-pages/man3/pthread_create.3.html).
void* thread_function(void* arg);
To be most portable and compliant with older C compilers, you should allocate your pthread_t array using malloc explicitly. Make sure to check for a NULL return value and free the memory later in this case. Or you can declare a maximum number of threads to allocate and use a constant array size.
pthread_t* thread_id = malloc(noOfThread*sizeof(pthread_t));
I originally had a global variable for my fibonacci variable array, but found out that is not allowed. I need to do elementary multithreading and handle race conditions, but I can't get past feeding an int as a void argument in pthread create. I've tried using a constant pointer with no luck. For some strange reason the void* gets past the first boolean test but not the else if:
$ gcc -o fibonacci fibonacci.c
fibonacci.c:22:16: warning: comparison between pointer and integer ('void *' and 'int')
else if (arg == 1)
~~~ ^ ~
1 warning generated.
My code is a mess and I am getting really confused because I have rewritten it so many times. If I cast all the args in my thread run function as ints I get a segmentation fault 11, which makes sense. All attempts at passing the i index by address and dereferencing it have failed, as it is a void and can't be used as an int. Can you suggest something else?
#include<stdio.h> //for printf
#include<stdlib.h> //for malloc
#include<pthread.h> //for threading
#define SIZE 25 //number of fibonaccis to be computed
int *fibResults; //array to store fibonacci results
void *run(void *arg) //executes and exits each thread
{
if (arg == 0)
{
fibResults[(int)arg] = 0;
printf("The fibonacci of %d= %d\n", (int)arg, fibResults[(int)arg]);
pthread_exit(0);
}
else if (arg == 1)
{
fibResults[(int)arg] = 1;
printf("The fibonacci of %d= %d\n", (int)arg, fibResults[(int)arg]);
pthread_exit(0);
}
else
{
fibResults[(int)arg] = fibResults[(int)arg -1] + fibResults[(int)arg -2];
printf("The fibonacci of %d= %d\n", (int)arg, fibResults[(int)arg]);
pthread_exit(0);
}
}
//main function that drives the program.
int main()
{
pthread_attr_t a;
fibResults = (int*)malloc (SIZE * sizeof(int));
pthread_attr_init(&a);
for (int i = 0; i < SIZE; i++)
{
pthread_t thread;
pthread_create(&thread, &a, run,(void*) &i);
printf("Thread[%d] created\t", i);
fflush(stdout);
pthread_join(thread, NULL);
printf("Thread[%d] joined & exited\t", i);
}
return 0;
}
You don't need the cast in the call to pthread_create() — the conversion to void * is automatic.
In the thread function, you could use
int i = *(int *)arg;
However, you've now got a synchronization problem; all the threads are using the same (pointer to the same) integer variable, and you can't predict which value they're going to see because of scheduling issues. The per-thread data needs to be 'per thread'.
So, there are various ways around that. In this context, I'd probably use
#include <stdint.h>
and in main():
pthread_create(&thread, &a, run, (void*)(uintptr_t)i);
and then in the thread function:
int i = (uintptr_t)arg;
Now the casts — the double cast even — is necessary. The cast to uintptr_t ensures the integer value is big enough to hold a pointer; the cast to void * is needed because there isn't an implicit cast from any integer type to void *. This ensures each thread function invocation has a different value. Sharing a pointer to an int means that everything is uncontrolled.
In the run() function you should do:
void *run(void *ptrarg) //executes and exits each thread
{
int arg = *((int *)ptrarg);
if (arg == 0)
....
....
and in rest of the run(), you don't need to cast the arg. Replace (int)arg with arg.
EDIT:
The way you are passing the argument to fun() while creating threads may cause race condition because all threads will be using same pointer. Check the #Jonathan's answer to avoid this problem.
#efuddy. Instead of (int)arg you should use (int *)arg to properly cast the **void pointer* void *arg
This question already has answers here:
Pass integer value through pthread_create
(4 answers)
Closed 5 years ago.
I am trying to understand following code. This code has no race conditions, but I cannot understand.
#include <stdio.h>
#include <pthread.h>
void *foo(void *vargp) {
int id;
id = (int)vargp;
printf("Thread %d\n", id);
}
int main() {
pthread_t tid[2];
int i;
for (i = 0; i < 2; i++)
pthread_create(&tid[i], NULL, foo, (void *)i);
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
How does the typecasting from int to void* work?
The race condition that is avoided is illustrated by this code, which is very similar to the original code, but subtly different, and the difference makes it incorrect:
/* Incorrect code! */
#include <pthread.h>
#include <stdio.h>
static void *foo(void *vargp)
{
int id = *(int *)vargp;
printf("Thread %d\n", id);
return 0;
}
int main(void)
{
pthread_t tid[2];
int i;
for (i = 0; i < 2; i++)
pthread_create(&tid[i], NULL, foo, &i); // Bad idea!
pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);
return 0;
}
Since the function foo takes a void * argument, it seems logical to pass the address of the int to it. However, this has a major problem:
There is no guarantee which order the threads will execute, nor when, so there's no way to know which values the threads will see.
Indeed, when I ran this code the first time, both threads reported 2.
The way around this is to not pass the address of i but to pass i by value. However, the argument is still supposed to be a void *, so the code casts i to a void * before calling pthread_create(), and the thread function undoes the cast to retrieve the value.
When I'm doing this, I also use <stdint.h> to make the uintptr_t type available, and I use:
int id = (uintptr_t)vargp;
and
pthread_create(&tid[i], NULL, foo, (void *)(uintptr_t)i);
That looks excessive and/or obsessive, but the uintptr_t cast ensures the integer is the same size as a pointer to avoid the 'cast to pointer from integer of different size' compiler warning (which, since I tell the compiler to treat all warnings as errors, is necessary for me to get the code to compile at all).
If you do pass a pointer-to-data to the thread function (foo in this discussion), you must ensure that each thread you create gets its own copy of the data unless that data is meant to be identical in each thread.
You can see this technique at work in POSIX threads — unique execution.
pthread_create(&Thread,NULL,ChildThread,(void *)100);
1) Can we pass the 4th argument of pthread_create as shown above? shouldn't it be a pointer variable?
Just an example (not meant to be correct way of doing it; but to serve as example code for anyone who want to play with it):
#include <stdio.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>
void *print_number(void *number) {
printf("Thread received parameter with value: %d\n", number);
return (void *)number;
}
int main(int argc, char *argv[]) {
pthread_t thread;
void *ret;
int pt_stat;
pt_stat = pthread_create(&thread, NULL, print_number, (void *)100);
if (pt_stat) {
printf("Error creating thread\n");
exit(0);
}
pthread_join(thread, &ret);
printf("Return value: %d\n", ret);
pthread_exit(NULL);
return 0;
}
This will lead to undefined behavior if the pointer value is greater then what an int can hold. See this quote from C99:
Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.
What (void *)100 means is take the integer value 100 and treat it as a pointer to some unspecified type of memory (i.e., (void *)). In that case, that means push the integer value 100 on the stack as an argument to pthread_create. Presumably, ChildThread casts the void * passed to it back to an int, then uses it as a number.
Fundamentally pointers are really just memory addresses. A memory address is just a number that describes a location in memory, so casting an int to a pointer of any type is legal. There are a few cases where casting an int to a pointer is absolutely the right thing to do and required, however, they tend to be rare. For example, if you are writing code for an embedded controller, and want write a driver for a memory mapped I/O device, then you might cast the device's base address as a pointer to an int or struct and then do normal C accesses through the pointer to access the device. Another example where casting ints to pointers, would be to implement the low-level virtual memory management routines to parcel out physical memory for an operating system.
The code you present is not uncommon and will work, assuming that the size of a pointer is at least big enough to hold the integer you are trying to pass. Most systems that implement pthread_create would probably have a 32-bit or 64-bit pointer, so your example is pretty likely to work. IMHO, it is a bit of an abuse, because 100 probably does not refer to a memory location in this case, and C does not guarantee that a void * is big enough to hold an int.
Taken from an excellent artice on POSIX Thread Progreamming . Must read for any newbie .
Example Code - Pthread Creation and Termination
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 5
void *PrintHello(void *threadid)
{
long tid;
tid = (long)threadid;
printf("Hello World! It's me, thread #%ld!\n", tid);
pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
pthread_t threads[NUM_THREADS];
int rc;
long t;
for(t=0; t<NUM_THREADS; t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
Explanation :
You can pass the 100 as the 4th argument to the pthread_create() . In the function PrintHello you can typecast the void* back into the correct type .
I have created a function for a thread, but I want to pass multiple parameters to the function.
Here's my source code :
#include "work.h"
#include <stdio.h>
#include <unistd.h>
#include <pthread.h> // compile with -lpthread
int count = 20;
void* ChildProc(void* arg)
{
int i;
for(i = 1; i <= count; i++)
{
printf("%s:%d from thread <%x>\n", arg, i, pthread_self());
DoWork(i);
}
return NULL;
}
void ParentProc(void)
{
int i;
for(i = count / 2; i > 0; i--)
{
printf("Parent:%d from thread <%x>\n", i, pthread_self());
DoWork(i);
}
}
int main(void)
{
pthread_t child;
pthread_create(&child, NULL, ChildProc, "Child");
ParentProc();
pthread_join(child, NULL); // make child a non-daemon(foreground) thread
}
Now how do I pass multiple parameter to ChildProc() method?
One way is either pass an array or a structure. But what if I want to pass multiple variables without an array or a structure?
A fast and junk answer is to create a struct to hold all parameter and pass its pointer
One way is either pass a array or a structure.
That's the way. Pointer to a structure, that is.
what if I want to pass multiple variables withous a array or a structure?
Then you're out of luck. Array or a pointer to a structure is what you need.
You can pass a void * buffer stream and if you know the lengths then you can access them. Its similar to implementation of GArrays.
How do you create them?
void *buffer = malloc(sizeofelements);
memcpy(buffer,element1,sizeof element1);
memcpy(buffer+sizeof element1,element2, sizeof element2);
NOTE: The above is not a compilable C code. You need to work on it.
You can use something of the above sort.
You can later access the variables as you already know the size