Using threads to output user input string

Hi, I am trying to have the user input a sentence and print it back to the user in the same order. But I have to use two threads, one that prints constants and one vowels, and it has to be in the right order. Could anyone point out something I may be missing. The order is not quite right.

In this part you are *not allowed* to use synchronization primitives such as mutexes for thread coordination. You can use sched_yield() to relinquish control of the CPU. You will have to investigate some of the other pthread functions available to you for this project. Look for examples online.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <sched.h>

#define MAX_SIZE 50
using namespace std;

//function prototypes
void *vowels(void*);
void *constants(void*);

//global vars
string strings1[MAX_SIZE];
string strings2[MAX_SIZE];
string strings3[MAX_SIZE]; 
int numWords = 0;

int main() 
{
	pthread_t vow;
	pthread_t con;
	
	char input[MAX_SIZE];
	char input2[MAX_SIZE];
	char* tokenize;
	int any;
	
	//prompt use to enter a string
	printf("Please enter a string.\n");
	
	cin.getline(input,MAX_SIZE,'.');

	tokenize = strtok(input, " ");

	while(tokenize)
	{			
		strings1[numWords] = tokenize;		
		strings2[numWords] = tokenize;
		strings3[numWords] = tokenize;

		tokenize = strtok(NULL, " ");

		numWords++;
	}
	
	/*for(int j = 0; j < numWords; j++)
		cout << strings1[j] << endl; */
		
	pthread_create(&vow, NULL, vowels, &any);
	pthread_create(&con, NULL, constants, &any);	
	
	pthread_join(con, NULL);
	pthread_join(vow, NULL);	

		
	return 0;
}
void *vowels(void* any)
{
	int position = 0;

	while( position < numWords )
	{
		string temp = strings1[position]; //store the current string into temp
	
		//check for vowels lower and upper
		if(temp[0] == 'a'|| temp[0] == 'e'|| temp[0] == 'i'|| temp[0] == 'o'
		|| temp[0] == 'u'|| temp[0] == 'A'|| temp[0] == 'E'|| temp[0] == 'I'
		|| temp[0] == 'O'|| temp[0] == 'U' && temp == strings3[position])
		{
			printf("vowel: %s\n", temp.c_str());

			position++;

		}
		else
		{
			position++;
			sched_yield();
		}
	}

	pthread_exit(0);	
}

void *constants(void* any)
{
	int position = 0;	

    while( position < numWords )
    {
        string temp = strings2[position]; //store the current string into temp
        
		//Check for constants lower and upper 
        if(temp[0] == 'b'|| temp[0] == 'c'|| temp[0] == 'd'|| temp[0] == 'f'
		|| temp[0] == 'g'|| temp[0] == 'h'|| temp[0] == 'j'|| temp[0] == 'k'
		|| temp[0] == 'l'|| temp[0] == 'm'|| temp[0] == 'n'|| temp[0] == 'p'
		|| temp[0] == 'q'|| temp[0] == 'r'|| temp[0] == 's'|| temp[0] == 't'
		|| temp[0] == 'v'|| temp[0] == 'w'|| temp[0] == 'x'|| temp[0] == 'y'
		|| temp[0] == 'z'|| temp[0] == 'B'|| temp[0] == 'C'|| temp[0] == 'D'
		|| temp[0] == 'F'|| temp[0] == 'G'|| temp[0] == 'H'|| temp[0] == 'J'
		|| temp[0] == 'K'|| temp[0] == 'L'|| temp[0] == 'M'|| temp[0] == 'N'
		|| temp[0] == 'P'|| temp[0] == 'Q'|| temp[0] == 'R'|| temp[0] == 'S'
		|| temp[0] == 'T'|| temp[0] == 'V'|| temp[0] == 'W'|| temp[0] == 'X'
		|| temp[0] == 'Y'|| temp[0] == 'Z' && temp == strings3[position])
        {
			printf("constant: %s\n", temp.c_str());

            position++;
        }
		else
		{
			position++;
			sched_yield();
		}
	}

	pthread_exit(0);
}


Last edited on
You didn't synchronize your threads. Where you have "sched_yield()" in code, you should acquire a mutex and notify the other thread through a condition variable (or use equivalent synchronization and notification primitives)
I can't use mutexes. I am new to this stuff how would I do that?
Can't use mutexes? Why, and what can you use then?
Just the sched yield().
It is for a class. Professors rules.
Rules:

In this part you are *not allowed* to use synchronization primitives such as mutexes for thread coordination. You can use sched_yield() to relinquish control of the CPU. You will have to investigate some of the other pthread functions available to you for this project. Look for examples online.
This sounds a little crazy.
If you're using Linux and running as root, you could actually achieve this: use sched_setscheduler() to set SCHED_FIFO, use sched_setparam() to give both threads the same realtime priority and use sched_setaffinity() to lock both threads to the same execution core.
I won't have root access.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <pthread.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sched.h>
#define MAX_SIZE 50

using namespace std;

//function prototypes
void *vowels(void*);
void *constants(void*);

//global vars
int numWords = 0;
string strings1[MAX_SIZE];
char dipthong[] = {'a', 'e', 'i', 'o', 'u'};

template <class iterit>
bool is_any_of(char myCh, iterit first, iterit last)
{
  while(first != last) {
    if ((myCh | 32) == *first)
      return true;
    ++first;
  }
  return false;
}

int main() 
{
  pthread_t vow, con;
  
  char input[MAX_SIZE], *tokenize;
  
  //prompt use to enter a string
  printf("Please enter a string.\n");
  cin.getline(input, MAX_SIZE);
  
  tokenize = strtok(input, " ");
  
  while(tokenize)
  {			
    strings1[numWords] = tokenize;		
    tokenize = strtok(NULL, " ");
    ++numWords;
  }
  
  pthread_create(&vow, NULL, vowels, NULL);
  pthread_create(&con, NULL, constants, NULL);	
  
  pthread_join(con, NULL);
  pthread_join(vow, NULL);	
  
  
  return 0;
}

void *vowels(void*)
{
  int k = 0;
  
  while( k < numWords )
  {
    string temp = strings1[k]; //store the current string into temp
    if (is_any_of(temp[0], dipthong, dipthong+5))
      printf("vowel: %s\n", temp.c_str());      
    else
      sched_yield();
    
    ++k;
  }
  
  pthread_exit(0);	
}

void *constants(void*)
{
  int t = 0;	
  
  while( t < numWords )
  {
    string temp = strings1[t]; //store the current string into temp
    //Check for constants lower and upper 
    if(!is_any_of(temp[0], dipthong, dipthong+5))
      printf("constant: %s\n", temp.c_str());
    else
      sched_yield();
    
    ++t;
  }
  
  pthread_exit(0);
}


E:
So you are trying to print each word whenever you find either a vowel or consonant? So if you ave the word "kate", it should print:

consonant: kate
vowel: kate
consonant: kate
vowel: kate

?

E: Or are you just checking if the first character is a consonant or vowel?
Last edited on
Topic archived. No new replies allowed.