Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

threading question

Status
Not open for further replies.

luvcloud

Programmer
Apr 23, 2002
24
MY
i read that C++ do not really have support for thread. Instead the API of the operating system is used. does this code below making an Win32 API call? the header imports process.h.

_beginthread(AcceptProc,0,&sockListen);
 
Do not use the API CreateThread, because it may cause memoryleaks in VC++. Instead, compile with one of the options /MT, /MTd, MD or /MDd. If you use one of these options, _beginthread or _beginthreadex is recognized.

Marcel
 
is there a difference between _beginthread, and the Worker/UI Thread provided in MFC?
 
Yes and no.
Yes: _beginthread is controlled by you, you can start anything as a thread.
No: both are threads within the same process.
Question is not quite clear.

Marcel
 
Actually what i don't understand is why C++ is said to have no support for threads, when there are a method to create thread which is the _beginthread. i'm trying to understand the way C++ handles multithreading as i need to compare it with C# for my college research project. I understand that most of the time, C++ is just making API calls or it uses class libraries (like MFC) that are wrapper around the API. so is _beginthread an API call too?
 
CreateThread is an API, which can be called from within any C-program, regardless of the compiler options.
_beginthread and _beginthreadex are C-runtime functions, which are normally not supported, only if you use one of the compiler options i mentioned in an earlier post. The reason for this is the synchronisation required in a multi-threaded environment.
Suppose two threads are running in the same process. The first thread is executing printf ( "aaaaaaaaaa" ); and the second printf ( "bbbbbbbbbb" ). If there is no synchronization, you might find something like "aaabbbbbbaaaaaaabbbb" in your output. Synchronized, the output will be "aaaaaaaaaabbbbbbbbbb" or "bbbbbbbbbbaaaaaaaaaa".

Marcel
Marcel
 
How can I run a class member method in a thread, in a win 32 console application?

Thanks,
Alberto H.
 
I would suggest a method pointer that you pass to the thread. See this quick example on method pointers. Cut and paste of the code should be fine

===========================================================================================
Code:
#include <iostream.h>

class c1{
public:
	c1::c1(){}
	void sample1();
	void sample2();
	void sample3();
	void sample4();
	void sample5();
	void sample6();

};

void c1::sample1(){cout<<&quot;sample1\n&quot;;}
void c1::sample2(){cout<<&quot;sample2\n&quot;;}
void c1::sample3(){cout<<&quot;sample3\n&quot;;}
void c1::sample4(){cout<<&quot;sample4\n&quot;;}
void c1::sample5(){cout<<&quot;sample5\n&quot;;}
void c1::sample6(){cout<<&quot;sample6\n&quot;;}

class c2{
public:
	c2::c2( void(c1::*ptr)() ):method_ptr(ptr){};
	void setmethod(void(c1::*ptr)());
	void fire();
private:

	void (c1::*method_ptr)();  
};


class c3{
public:
	c3::c3():method_ptr(fxna){};
	void setmethod(int x);
	void fxna(){cout<<&quot;fxna\n&quot;;}
	void fxnb(){cout<<&quot;fxnb\n&quot;;}
	void fxnc(){cout<<&quot;fxnc\n&quot;;}
private:

	void (c3::*method_ptr)();  
};

void c2::fire(){c1 temp;(temp.*method_ptr)();}
void c2::setmethod(void(c1::*ptr)()){method_ptr=ptr;}
void c3::setmethod(int x){
	switch(x)
	{ 
	case 1:method_ptr = fxna;break;
	case 2:method_ptr = fxnb;break;
	case 3:
	default:method_ptr = fxnc;break;
	}
}


int main()
{
	c2 mc2(&c1::sample1);

	mc2.fire();
	mc2.setmethod(&c1::sample2);
	mc2.fire();
	mc2.setmethod(&c1::sample3);
	mc2.fire();
	mc2.setmethod(&c1::sample4);
	mc2.fire();
	mc2.setmethod(&c1::sample5);
	mc2.fire();
	mc2.setmethod(&c1::sample6);
	mc2.fire();

	c3 mc3;

	cout<<&quot;\n\n\n\nbeginning\n\n\n\n&quot;;
	mc3.setmethod(1);
	mc3.setmethod(2);
	mc3.setmethod(3);



	return 0;
}

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top