-
-
Save jimmy-ly00/41a229d745ce90a24ddc2636dee7cd6a to your computer and use it in GitHub Desktop.
Two experimental examples of dynamically creating threading per function call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Takes input and split messages into 5 bytes. Delay each message per thread with known number of messages. Potentially | |
// could be used as a stop-n-go mixer with an exponential distributed delay | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <string.h> | |
#include <unistd.h> | |
struct delay_args { | |
pthread_t delaypt; | |
unsigned int delay; | |
char message[6]; | |
}; | |
void* delay_message(void *arguments){ | |
struct delay_args *args = arguments; | |
sleep(args->delay); | |
printf("shuffled message: %s\n", args->message); | |
} | |
int main(){ | |
time_t t; | |
srand((unsigned) time(&t)); | |
printf("Input message: "); | |
char buffer[100]; | |
while(1){ | |
fgets(buffer,100,stdin); | |
int n = strlen(buffer); | |
int diff = (n-1) % 5; | |
struct delay_args *args = malloc(sizeof (struct delay_args)*n); | |
if(diff != 0){ | |
memset(&buffer[n-1], '0', 5-diff ); | |
buffer[n+5-diff-1]='\0'; | |
} | |
int m = strlen(buffer); | |
for(int i=0; i<(m/5); i++){ | |
strncpy(args[i].message, buffer+(i*5), 5); | |
args[i].message[5] = '\0'; //null terminator | |
args[i].delay = rand() % 5; | |
printf("message: %s and time: %d\n", args[i].message, args[i].delay); | |
if(pthread_create( &args[i].delaypt, NULL, delay_message, (void *)&args[i]) != 0) | |
printf("Error in creating thread!"); | |
} | |
for(int i=0; i<(m/5); i++){ | |
pthread_join(args[i].delaypt, NULL); | |
} | |
free(args); | |
printf("Input message: "); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// creates a calculation per thread with unknown number of calculations. Problem is to deal with collection and cleaning | |
// of seperate threads. pthread_join will not work. A possible solution is to use set pthread initialiser to detach/use pthread_detach | |
// but needs the main thread to be running to see the results. Stil an outstanding problem | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <pthread.h> | |
#include <string.h> | |
#include <unistd.h> | |
struct args_struct { | |
pthread_t pt; | |
int random; | |
}; | |
void* calculate(void *arguments){ | |
struct args_struct *args = arguments; | |
sleep(args->random); | |
printf("Number: %d. %s\n", args->random, "Calculations completed, thanks for waiting!"); | |
if (args->random != 2) free(args); | |
} | |
int main(){ | |
time_t t; | |
srand((unsigned) time(&t)); | |
while(1){ | |
struct args_struct *args = malloc(sizeof (struct args_struct)); //create new thread per calculation | |
args->random = rand() % 10; | |
if(pthread_create( &args->pt, NULL, calculate, (void *)args) != 0) | |
printf("Error in creating thread!\n"); | |
if (args->random == 2 ){ //random condition in exiting loop | |
printf("Number is %d. Exiting program and joining remainder threads still running...\n", args->random); | |
sleep(10) // AFAIK no proper way to join these threads | |
free(args); | |
return 0; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment