URGENT Scheduling Mutilthreads

Hi all,

i am ecnountering a very complicated (atleast for me) problem with threads.

my main function looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(int argc, char**argv)
{
	checkArgs(argc, argv);
	vector<thread> threadPool;

	
	for (int i = 0; i < anzahlAlarms; i++){ /* (1) */
		threadPool.push_back(std::thread(alarm, (i + 1)));
	}

	for (auto& thread : threadPool){
		thread.join();
	}

	cout << "Done." << endl;
	return(0);
}


At (1) i am creating several threads and pushing them into my vector, whereas my alarm function looks like the following:


1
2
3
4
5
6
7
8
9
10
11
12
void alarm(int alarmNumber)
{
	int run = 1;
	chrono::microseconds duration(alarmNumber * globalScale);
	do
	{
		thread_mutex.lock();
		cerr << "Alarm id: " << alarmNumber << " Cycle " << run << "\n";
		thread_mutex.unlock();
		
	} while (run++ < 20);
}


well i want to schedule the threads and have the following output on my console:

/* first round */
alarm id 1 cycle 1
/* second round */
alarm id 1 cycle 2
alarm id 2 cycle 1
/* third round */
alarm id 1 cycle 3
alarm id 2 cycle 2
alarm id 3 cycle 1

and so on...

Working with Mutexes as in the following won't help either because for example after the first thread throws out his message, all other threads "fight" to show their on, as soon as the mutex is unlocked!

can anyone help me how to schedule the threads?

best greetings,
yoxus
Last edited on
The scheduling of threads is inherently non-deterministic. Why do you want to do this, and does it have to be with threads?
Topic archived. No new replies allowed.