Multithreading in C++

Aug 2, 2008 at 1:12am
Hi all,
I'm a beginner programer in VC++. I have two functions that I'm calling from the main function. I want to control the time that each function run for. I'm illustrarting below with the following simple code:

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
#include <stdafx.h>
#include <stdlib.h>
#include <windows.h>
#include <set>
#include <conio.h>
#include <iostream>

using namespace std;

int a,b,c,d;
int function1(int, int);
int function2(int);


int main(){

	cout << "Enter a: ";
	cin >> a;
	cout << "Enter b: ";
	cin >> b;
	function1(a,b);
	function2(c);
	
	return 0;
} /* End of Main */

int function1(int a, int b){

	c = (2*(a+b))-(3*b);
	cout << "c= " << c << "\n";
	return c;
} /* End of function1 */

int function2(int c){
	
	d = (2*c)+(c-3);
	cout << "d= " << d << "\n";

	return d;
}/* End of function2 */


I want for instance to run function1 for 10ms and function2 for 50 ms. I know one of the ways to do this is multithreading using Mutex, yet I have no experience in multithreading. I tried to read about it but honestly it was beyond my knowledge of the language to understand the sample code. So I was hoping to find an easy way maybe using timers, waitbar timers or anything else. I don't want to use Sleep(ms) to achieve as I want to have full control of the running time.

Any help would be appreicated.
Last edited on Aug 2, 2008 at 1:13am
Aug 2, 2008 at 7:09am
Well, let me dig up my code and find an example of threading...
I think this should work. Let me know if it doesn't and I'll give you a more literal copy:
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
#include <windows.h>

struct parameters{
	//whatever parameters thread0 needs
};

DWORD thread0(void *params){
	parameters *a=(parameters *)params;
	//put parameters into separate variables, if you want
	//thread0's body
}

//I like to use this function to start threads more easily
HANDLE newThread(LPTHREAD_START_ROUTINE routine,LPVOID parameters,int priority=0){
	HANDLE res=CreateThread(0,0,routine,parameters,0,0);
	if (priority)
		SetThreadPriority(res,priority);
	return res;
}

int main(){
	parameters *thread0data=new parameters;
	//initialize thread0data
	HANDLE *thread0handle=newThread(thread0,thread0data);
	//Let's suppose you want to wait 100 ms (10 is below the granularity of Sleep(), if I remember correctly)
	Sleep(100);
	//http://msdn.microsoft.com/en-us/library/ms684335(VS.85).aspx
	thread0handle=OpenThread(THREAD_TERMINATE,0,GetThreadId(thread0handle));
	if (!thread0handle){
		//OpenThread() has failed. Deal with the error here.
	}
	//http://msdn.microsoft.com/en-us/library/ms686717(VS.85).aspx
	TerminateThread(thread0handle,0);
	//do other stuff
	return 0;
}

For more information, see http://msdn.microsoft.com/en-us/library/ms684847(VS.85).aspx
Aug 3, 2008 at 8:03pm
Aug 5, 2008 at 3:31pm
Thanks helios for the info, but where can I specify the sampling time (frequency) that I want every thread to run. Is it possible to do it?


And thanks Zaita for the link
Aug 5, 2008 at 8:03pm
What exactly do you mean by "sampling time (frequency)"? This doesn't take samples of how long it has passed. It's simply one thread executing while another waits a certain amount of time and then immediately kills the other thread. There's no sampling.
Aug 7, 2008 at 1:50pm
Hi, I'm a newbie in C++ programming on Win Operative Systems.
I'm a little confused about Multithreading in Visual C++. I wrote some image processing libraries on Linux using some computer Vision and Image Compressing Libraries those are portable. Now I reduced my portabiliy problems on Threading code... In Linux I've got Posix Thread, but in Visual C++ I've got .NET Threading system, Multithreading C with Win32 (from COM i suppose (?)) and Multithreading with MFC... What most considereable differences are between these systems... What would you suggest me for multithreading image processing?
Aug 7, 2008 at 7:44pm
Topic archived. No new replies allowed.