Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- #define NUM_THREADS 4
- int sharedInt;
- typedef struct {
- const char* userInput;
- }sharedValue;
- void* threadListen(void* threadID){
- printf("thread %d live\n", threadID);
- return 0;
- }
- void* printText(void* foo){
- sharedValue *structure = (sharedValue*)foo;
- printf("Thread received text: %s\n", foo);
- return 0;
- }
- void* mainThread(void* foo){
- pthread_t threads[NUM_THREADS];
- int rc, i;
- for(i = 0; i<NUM_THREADS;i++){
- //Printing Thread
- if(i == 3)
- rc = pthread_create(&threads[i], NULL, printText, (void *)i);
- //Consumer Threads
- else
- rc = pthread_create(&threads[i], NULL, threadListen, (void *)i);
- }
- if(rc){
- printf("pthread_create ERROR");
- return -1;
- }
- printf("Please enter text: ");
- char textEntered[100];
- scanf("%99[^\r\n]", textEntered);
- printf("%s", textEntered);
- for (i = 0; i < NUM_THREADS; i++) {
- pthread_join(threads[i], NULL);
- }
- return 0;
- }
- int main(int argc, const char* argv[]){
- int rc;
- pthread_t main_thread;
- //Producer Thread(Main Thread)
- rc = pthread_create(&main_thread, NULL, mainThread, NULL);
- if(rc){
- printf("pthread_create ERROR");
- return -1;
- }
- pthread_join(main_thread, NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement