Threading and loops

Hi, I'm trying to get my threads to run at the same time but I need for them to display different values and delay for different periods of time. I've tried loops but its not getting me anywhere. Any pointers would be greatly appreciated.

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
// lab5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <process.h>
using namespace std;

int ids [5] = { 100, 200, 300, 400, 500 };
int delays [5] = { 500, 600, 700, 800, 900 };

struct ThreadArgs
{
	int id;
	int delay;
};


unsigned __stdcall MyThread(void *data)
{
	for(int y = 0; y < 10; ++y)
	{
		ThreadArgs *args = (ThreadArgs *) data;
		cout << args->id << endl;
		Sleep(args->delay);
	}
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int n = 0;
	if(n < 10)
	{	
		ThreadArgs args;
		args.id = ids[0];
		args.delay = delays[0];
		HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
		WaitForSingleObject(hThread, INFINITE);
	}
	if(n < 10)
	{	
		ThreadArgs args;
		args.id = ids[1];
		args.delay = delays[1];
		HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
		WaitForSingleObject(hThread, INFINITE);
	}
	if(n < 10)
	{
		ThreadArgs args;
		args.id = ids[2];
		args.delay = delays[2];
		HANDLE hThread1 = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
		WaitForSingleObject(hThread1, INFINITE);
	}
	if(n < 10)
	{	
		ThreadArgs args;
		args.id = ids[3];
		args.delay = delays[3];
		HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
		WaitForSingleObject(hThread, INFINITE);
	}
	if(n < 10)
	{	
		ThreadArgs args;
		args.id = ids[4];
		args.delay = delays[4];
		HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
		WaitForSingleObject(hThread, INFINITE);
	}
	if(n < 10)
	{	
		ThreadArgs args;
		args.id = ids[5];
		args.delay = delays[5];
		HANDLE hThread = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
		WaitForSingleObject(hThread, INFINITE);
	}
	return 0;
}
Right now it appears that it is executing synchronously, because each if block waits for the thread to complete before moving on to the next if block.

If you want all the threads to run at the same time, then get rid of the if statements, define your thread with different names, run them all, and wait for them all.

By the way, you are printing before you sleep, so you will get a lot of prints right at the beginning, and then each of the threads will quietly complete over time.
If I get rid of the if loops I can't get the ThreadArgs structure to give the different values to each thread. How can I get the pointers to give the different values without loops?
1
2
3
4
5
6
7
8
9
10
//...
HANDLE hThread[MAX_THREAD_COUNT];
for(i = 0; i < MAX_THREAD_COUNT; ++i)
{
   ThreadArgs args;
   args.id = ids[i];
   args.delay = delays[i];
   hThread[i] = (HANDLE) _beginthreadex(NULL, 0, MyThread, (void *) &args, 0, NULL);
}
//Then whatever the waitForAllThread call is... passing the hThread... 


I haven't used this API in forever (we use boost in our c++)
Last edited on
Topic archived. No new replies allowed.