C passing shared variables into pthreads - c

I wrote the following program to test some assumptions I have about pthreads:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
typedef struct threadArgs * ThreadArgs;
void *foo( void * argStruct );
struct threadArgs{
int pingBool;
};
int main(int argc, char const *argv[]){
pthread_t t1;
int pingBool = 1;
int ch;
ThreadArgs args = (ThreadArgs)malloc(sizeof(struct threadArgs));
args->pingBool = pingBool;
pthread_create(&t1, NULL, foo, args);
while((ch = getchar()) != EOF ){
if( ch == 'q' ){
pingBool = 0;
printf("pingBool now 0\n");
break;
}
}
pthread_join(t1, NULL);
printf("whole program terminating\n");
return 0;
}
void *foo( void * argStruct ){
ThreadArgs args = argStruct;
while( args->pingBool == 1 ){
printf("t1 still going\n");
sleep(1);
}
printf("t1 finished\n");
return NULL;
}
Basically, it sets a variable int pingBool = 1 in main(), passes it into a thread which loops until pingBool == 0. main() then sets pingBool = 0, however, the thread continues to run. I'm pretty sure this is happening because pingBool is getting copied when it is put into the struct and only the copy local to main() is being modified.
Is there a way to share and modify variables between threads like this? I am aware that I can simply make pingBool a global variable rather than pass it into foo(). However, I would prefer not to do that.
Note: If you want to test the code you need to compile with -pthread or -lpthread depending on your compiler. i.e. gcc -Wall -Werror -pthread -o test test.c

You already are sharing the variable between two threads - it's just that you're not referring to the shared copy in main. To refer to the shared copy, main needs to use args->pingBool.
However, your code will then not be correct, because it will have a data race - two threads aren't allowed to access a shared variable without using a synchronisation function. To fix that, you can use a mutex:
struct threadArgs {
pthread_mutex_t lock;
int pingBool;
};
It will need to be initialised:
ThreadArgs args = (ThreadArgs)malloc(sizeof(struct threadArgs));
pthread_mutex_init(&args->lock, NULL);
main should lock the mutex when changing args->pingBool:
if ( ch == 'q' ) {
pthread_mutex_lock(&args->lock);
args->pingBool = 0;
pthread_mutex_unlock(&args->lock);
printf("pingBool now 0\n");
break;
}
and the thread should lock it while reading it:
pthread_mutex_lock(&args->lock);
while ( args->pingBool == 1 ) {
pthread_mutex_unlock(&args->lock);
printf("t1 still going\n");
sleep(1);
pthread_mutex_lock(&args->lock);
}
pthread_mutex_unlock(&args->lock);

Nevermind, solved it myself. The trick was to pass in a pointer to the original variable and access the value of that pointer from foo(). Ammended code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
typedef struct threadArgs * ThreadArgs;
void *foo( void * argStruct );
struct threadArgs{
int *pingBool; // Now a pointer, not an int
};
int main(int argc, char const *argv[]){
pthread_t t1;
int pingBool = 1;
int ch;
ThreadArgs args = (ThreadArgs)malloc(sizeof(struct threadArgs));
args->pingBool = &pingBool; // pass in a pointer to the variable, rather than the variable itself
pthread_create(&t1, NULL, foo, args);
while((ch = getchar()) != EOF ){
if( ch == 'q' ){
pingBool = 0;
printf("pingBool now 0\n");
break;
}
}
pthread_join(t1, NULL);
printf("whole program terminating\n");
return 0;
}
void *foo( void * argStruct ){
ThreadArgs args = argStruct;
int * pingBool = args->pingBool;
while( *pingBool == 1 ){ // access the value of the pointer
printf("t1 still going\n");
sleep(1);
}
printf("t1 finished\n");
return NULL;
}

Try passing args by reference to pthread_create(). It would look like:
pthread_create(&t1, NULL, foo, &args);

Related

Problem with running a function in 2 threads in C

I have written a program with a function that prints a character received as an argument continuously, and made 2 threads running that function. The program runs as intended and keeps printing 2 characters interlaced indefinitely. Here is the code:
#include <pthread.h>
#include <stdio.h>
void* print_char(void* th_param) {
char* ch = (char*)th_param;
while(1)
printf("%c", *ch);
return NULL;
}
int main() {
char* c1 = "a";
char* c2 = "b";
pthread_t th_id;
pthread_create(&th_id, NULL, &print_char, (void*)c1);
pthread_t th_id2;
pthread_create(&th_id2, NULL, &print_char, (void*)c2);
return 0;
}
Now I have to modify this program so the print function takes a number argument as well, and prints the character that number of times. What I try is this:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct Params {
char* ch;
int num;
};
void* print_char(void* th_param) {
int i;
struct Params* par = (struct Params*)th_param;
for(i=0; i<par->num; i++) {
printf("%s", par->ch);
}
return NULL;
}
int main() {
struct Params* p1 = (struct Params*)malloc(sizeof(struct Params));
p1->ch = "a"; p1->num = 5;
struct Params* p2 = (struct Params*)malloc(sizeof(struct Params));
p2->ch = "b"; p2->num = 10;
pthread_t th_id;
pthread_create(&th_id, NULL, &print_char, (void*)p1);
pthread_t th_id2;
pthread_create(&th_id2, NULL, &print_char, (void*)p2);
while(1) {}
return 0;
}
But to no avail. Doesn't print a single character, the cursor just stands there blinking doing nothing. I have tried tampering with the code for hours and nothing has worked.
Note that there's not a problem with the struct, because the program works if I remove the for loop and put while(1) instead, and prints the character given to par->ch infinite times again.
The problem is buffered I/O.
Your main()-function loops infinitely, so the process doesn't terminate properly (only by being killed via an external signal), so the stdout-buffer is never flushed by regular process termination.
Since stdout is line-buffered by default, and your printf()s don't contain any newlines, and the amount of characters you try to print is smaller than the I/O-buffer, your program never writes out the buffer, so nothing is printed.
One approach would be to add fflush(stdout); after the printf()s in your thread function, or include a '\n' in the output.
Another (more sane) approach would be to have main() actually wait for the threads to finish, and gracefully terminate when they are done:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct Params {
char* ch;
int num;
};
void* print_char(void* th_param) {
int i;
struct Params* par = (struct Params*)th_param;
for(i=0; i<par->num; i++) {
printf("%s", par->ch);
//fflush(stdout);
}
return NULL;
}
int main() {
struct Params* p1 = (struct Params*)malloc(sizeof(struct Params));
p1->ch = "a"; p1->num = 5;
struct Params* p2 = (struct Params*)malloc(sizeof(struct Params));
p2->ch = "b"; p2->num = 10;
pthread_t th_id;
pthread_create(&th_id, NULL, &print_char, (void*)p1);
pthread_t th_id2;
pthread_create(&th_id2, NULL, &print_char, (void*)p2);
//while(1) {}
pthread_join(th_id, NULL);
pthread_join(th_id2, NULL);
free(p1); //make LeakSanitizer happy, don't leak memory
free(p2);
puts(""); //one newline for command-line beautification
fflush(stdout); //redundant after the puts(""), unless stdout is redirected to a non-interactive stream
return 0;
}
Thanks everyone, using fflush did the job.
To answer the person who asked what platform I ran this on, I was running this on Linux terminal. This was the homework for my OS lab class and pthread_create is all they have taught us so far, pthread_join and more thread management stuff come in later sessions.
Because of your while loop, stdout never get's flushed
Try this :
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
struct Params {
char* ch;
int num;
};
void* print_char(void* th_param) {
int i;
struct Params* par = (struct Params*)th_param;
for(i=0; i<par->num; i++) {
printf("%s", par->ch);
}
return NULL;
}
int main() {
struct Params* p1 = malloc(sizeof(struct Params));
p1->ch = "a"; p1->num = 5;
struct Params* p2 = malloc(sizeof(struct Params));
p2->ch = "b"; p2->num = 10;
pthread_t th_id;
pthread_create(&th_id, NULL, &print_char, p1);
pthread_t th_id2;
pthread_create(&th_id2, NULL, &print_char, p2);
fflush(stdout);
while(1) {;}
return 0;
}

Bounded buffer sharing with Pthread and mutex locks busy waiting

I am trying to create a producer consumer queue, using mutex locks, creating busy waiting between threads. My Main file takes X amount of integer arguments, pushes them onto a BOUNDED BUFFER of size 50. I am using a while loop to do this since you do not know the amount before hand. I am not sure when and where to create my producer thread.
NOTE: Main is a "producer" in the sense it fills the buffer, but my actual producer function is going to pass onto my consumer function later in my code, so disregard the names. Main is going to "Produce" numbers by pushing and producer is going to pop those numbers for later use. My question is where and when do I make my Pthread_create in my code for producer and am I using the Mutex locks correctly to have synchronization between the two threads?
#include <pthread.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#define BUFFER_SIZE (50)
typedef struct {
int buffer[BUFFER_SIZE];
int count;
int top;
int next;
pthread_mutex_t count_lock;
} prodcons;
void pc_init(prodcons *pc);
int pc_pop(prodcons *pc);
void pc_push(prodcons *pc, int val);
void factor2pc(prodcons *pc, int number);
void *producer(void *data);
void *consumer(void *data);
int main(int argc, char *argv[])
{
int index = 1;
int num;
prodcons pc_nums;
//pthread_t tid[argc - 1];
pthread_t tid;
pthread_attr_t attr;
if (argc < 2) {
fprintf(stderr, "usage: No arguments\n");
return -1;
}
if (atoi(argv[1]) <= 0)
{
fprintf(stderr, "%d not > 0 or you must provide a positive integer.\n", atoi(argv[1]));
return -1;
}
pthread_attr_init(&attr);
pc_init(&pc_nums);
//DO I PUT THIS HERE or WHILE LOOP?
pthread_create(&tid, &attr, *producer, &pc_nums);
while (index < argc)
{
num = atoi(argv[index]);
pc_push(&pc_nums, num);
index++;
}
}
void *producer(void *data)
{
prodcons *dataStruct = data;
while (dataStruct->count < BUFFER_SIZE)
{
number = pc_pop(data);
//This print is just here to make sure I am correctly "poping" from buffer
printf("%d\n", number);
}
}
void pc_init(prodcons *pc)
{
pc->count = 0;
pc->top = 0;
pc->next = 0;
if (pthread_mutex_init(&pc->count_lock, NULL) != 0)
{
printf("\n mutex init has failed\n");
}
}
int pc_pop(prodcons *pc)
{
int val;
pthread_mutex_lock(&pc->count_lock);
if (pc->count > pc->top)
{
val = pc->buffer[pc->count];
printf("%d\n", val);
pc->buffer[pc->count] = 0;
pc->count--;
}
pthread_mutex_unlock(&pc->count_lock);
return val;
}
void pc_push(prodcons *pc, int val)
{
pthread_mutex_lock(&pc->count_lock);
if (pc->count < BUFFER_SIZE)
{
pc->buffer[pc->count] = val;
pc->count++;
}
pthread_mutex_unlock(&pc->count_lock);
}
My question is where and when do I make my Pthread_create in my code for producer and am I using the Mutex locks correctly to have synchronization between the two threads?
As long as all is properly initialized and synchronized, you can put the pthread_create() call wherever you want, including where it's placed in the given program. But at least two things are wrong:
pc_pop() behaves undefined (by return of an uninitialized value) if there is no number in the buffer to pop.
Since dataStruct->count is accessed by producer() without locking, the declaration should be _Atomic(int) count;.

How can I have a shared counter in multithreading using structs?

I'm pretty new with multithreading and I was trying to increment a shared counter whithout using global variables, my goal is try to maximize the concurrency among the different threads and increment the variable until a number I give in arguments... Sorry if is a lame question, but I would like a help here, when I compile my code and run it i get a segmentation fault... I think the error is in the variable count that I create and the shared counter!
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
typedef struct {
long *cnt; /* pointer to shared counter */
long n; /* no of times to increment */
int nr;
pthread_t id; /* application-specific thread-id */
} targ_t;
void *sfun(void *arg) {
targ_t *est = (targ_t *) arg;
here:
pthread_mutex_lock(&mutex);
(*(est->cnt))++;
pthread_mutex_unlock(&mutex);
if(*(est->cnt)<est->n)
goto here;
return NULL;
}
int main(int argc, char *argv[]){
targ_t real[3];
int c=0;
long count=0;
real[0].cnt=&count;
pthread_mutex_init(&mutex, NULL);
for(c=0;c<3;c++){
real[c].n=atoi(argv[1]);
real[c].nr=c+1;
pthread_create(&real[c].id,NULL,&sfun,&real[c]);
}
for(c=0;c<3;c++){
pthread_join(real[c].id,NULL);
}
pthread_mutex_destroy(&mutex);
printf("OVERALL %lu\n", count);
return 0;
}
TY in advance.
There are a number of problems identified in the comments:
Writing out loops with a label here: and a goto here; is not a particularly good idea. There are occasions (some, but not many, occasions) when it is appropriate to use goto — this is not one of those rare occasions.
You don't actually validate that your code was given an argv[1] to convert; could it be that you forgot to pass that argument?
However, your primary problem is that you initialize real[0].cnt but you do not initialize real[1].cnt or real[2].cnt, so those threads are accessing who knows what memory — it might be that they're using null pointers, or they might be pointers to anywhere in memory, allocated or not, aligned or not, writable or not.
You're also missing <stdlib.h>
You're testing *(est->cnt) outside the scope of mutual exclusion.
This code fixes those and some other issues:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
typedef struct
{
long *cnt; /* pointer to shared counter */
long n; /* no of times to increment */
int nr;
pthread_t id; /* application-specific thread-id */
} targ_t;
static void *sfun(void *arg)
{
targ_t *est = (targ_t *)arg;
while (1)
{
pthread_mutex_lock(&mutex);
long cval = *est->cnt;
if (cval < est->n)
++*est->cnt;
pthread_mutex_unlock(&mutex);
if (cval >= est->n)
break;
}
return NULL;
}
int main(int argc, char *argv[])
{
targ_t real[3];
long count = 0;
if (argc != 2)
{
fprintf(stderr, "Usage: %s count\n", argv[0]);
return(EXIT_FAILURE);
}
for (int c = 0; c < 3; c++)
{
real[c].cnt = &count;
real[c].n = atoi(argv[1]);
real[c].nr = c + 1;
if (pthread_create(&real[c].id, NULL, &sfun, &real[c]) != 0)
break;
}
for (int c = 0; c < 3; c++)
pthread_join(real[c].id, NULL);
pthread_mutex_destroy(&mutex);
printf("OVERALL %lu\n", count);
return 0;
}
When run (for example, the program is pth59):
$ pth59 100
OVERALL 100
$
Before moving the test (now on cval) so that the read of *est->cnt was done inside the scope of the mutex, I got an output OVERALL 102 from the same command line. It is important to access shared variables with proper mutual exclusion, even if it is read-only access.

Program won't execute pthread

I have to design a multi-threaded application involved with the consumer-producer problem. So far, I've been trying to get the Pthreads to work correctly before I try to implement my solution. But, my program won't even load up the the function on my pthread. I'm not sure what I'm doing wrong here.
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
void* Producer(void *arg)
{
printf("\nEntered Producer\n");
int i, item, index;
index = (int)arg;
FILE *f = fopen(bookorders, "r");
char c = fgetc(f);
int z =0;
while (c!=EOF) {
char * buffer = (char *)malloc(1000);
while (c!='\n') {
*(buffer+z) = c;
z++;
c = fgetc(f);
}
char delim[2] = "|";
printf("%s\n", buffer);
}
}
int main(int argc, const char * argv[])
{
pthread_t Produc;
pthread_create(&Produc, NULL, Producer, NULL);
return 0;
}
I guess my big question is what is the proper process of creating a pthread and then getting it to run a function, which in this case is my Producer function
What's happening here is your main thread calls pthread_create and just returns and since the main thread exits, your Production thread also exits. What you need to do is, instruct the main thread to wait for the Produc thread to finish executing.
int main(){
//Call pthread_create
pthread_join(Produc, NULL) ;
return 0;
}

create thread - passing arguments

I am attempting on creating multiple threads that each thread calculates a prime. I am trying to pass a second argument to a function using thread create. It keeps throwing up errors.
void* compute_prime (void* arg, void* arg2)
{
here is my main() with the create thread. &primeArray[i] after &max_prime is giving me the errors.
for(i=0; i< num_threads; i++)
{
primeArray[i]=0;
printf("creating threads: \n");
pthread_create(&primes[i],NULL, compute_prime, &max_prime, &primeArray[i]);
thread_number = i;
//pthread_create(&primes[i],NULL, compPrime, &max_prime);
}
/* join threads */
for(i=0; i< num_threads; i++)
{
pthread_join(primes[i], NULL);
//pthread_join(primes[i], (void*) &prime);
//pthread_join(primes[i],NULL);
//printf("\nThread %d produced: %d primes\n",i, prime);
printf("\nThread %d produced: %d primes\n",i, primeArray[i]);
sleep(1);
}
the error i get is:
myprime.c: In function âmainâ:
myprime.c:123: warning: passing argument 3 of âpthread_createâ from incompatible pointer type
/usr/include/pthread.h:227: note: expected âvoid * (*)(void *)â but argument is of type âvoid * (*)(void *, void *)â
myprime.c:123: error: too many arguments to function âpthread_createâ
It works fine if i take out the second argument.
You can only pass a single argument to the function that you are calling in the new thread. Create a struct to hold both of the values and send the address of the struct.
#include <pthread.h>
#include <stdlib.h>
typedef struct {
//Or whatever information that you need
int *max_prime;
int *ith_prime;
} compute_prime_struct;
void *compute_prime (void *args) {
compute_prime_struct *actual_args = args;
//...
free(actual_args);
return 0;
}
#define num_threads 10
int main() {
int max_prime = 0;
int primeArray[num_threads];
pthread_t primes[num_threads];
for (int i = 0; i < num_threads; ++i) {
compute_prime_struct *args = malloc(sizeof *args);
args->max_prime = &max_prime;
args->ith_prime = &primeArray[i];
if(pthread_create(&primes[i], NULL, compute_prime, args)) {
free(args);
//goto error_handler;
}
}
return 0;
}
In case of std::thread, the user can pass arguments to the thread function in the following method
std::thread(funcName,arg1,arg2);
for instance,
//for a thread function,
void threadFunction(int x,int y){
std::cout << x << y << std::endl;
}
// u can pass x and y values as below
std::thread mTimerThread;
mTimerThread = std::thread(threadFunction,1,12);
This is the code of Manakarse , everything is really good but you need a
pthread_join(thread[i],NULL)
just to be sure all of threads will successfully execute before end of main thread("main will "waiting" while all of threads aren't finished yet)
;
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct
{
int stop_flag;
char name[30];
} _process_data;
typedef struct
{
int meter_no;
int port_no;
} _process_control;
typedef struct
{
_process_data *process_data;
_process_control *process_control;
} compute_prime_struct;
void *compute_prime (void *args)
{
compute_prime_struct *actual_args = args;
printf("actual_args->process_data->stop_flag [%d]\n",actual_args->process_data->stop_flag);
printf("actual_args->process_data->name [%s]\n",actual_args->process_data->name);
printf("actual_args->process_control->meter_no [%d]\n",actual_args->process_control->meter_no);
printf("actual_args->process_control->port_no [%d]\n",actual_args->process_control->port_no);
free(actual_args);
return 0;
}
void fill_data(_process_data *process_data,_process_control *process_control)
{
process_data->stop_flag=1;
process_data->name[0]='P';
process_control->meter_no=6;
process_control->port_no=22;
pthread_t tid;
compute_prime_struct *args = malloc(sizeof (*args));
args->process_data = malloc(sizeof (*args->process_data));
args->process_control = malloc(sizeof (*args->process_control));
memcpy (args->process_data, process_data, sizeof (args->process_data));
memcpy (args->process_control, process_control, sizeof (*args->process_control));
if(pthread_create(&tid, NULL, compute_prime, args))
{
free(args);
printf("Error here");
}
sleep(1);
}
int main()
{
_process_data process_data;
_process_control process_control;
fill_data(&process_data,&process_control);
return 0;
}

Resources