Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi All,

I am using the method _beginthreadex to create a thread in Visual Studio 2008 compiling with /MD option as suggested by MSDN.

Code :
ifdef _WINDOWS
_beginthreadex(NULL,0, &CMyClass::threadFunction, NULL, 0, NULL );
#endif


for running i am installing Visual Studio 2008 restributable package but my application crashes. if i am not using this method _beginthreadex, then my application runs correclty,

Please let me know what mistake am i doing,

Thanks,
Yash,
Posted
Comments
[no name] 10-Jul-12 16:21pm    
It could be crashing because of something in CMyClass::threadFunction...
Yashusid 10-Jul-12 17:17pm    
Hi Wes Aday,

You are absolutely correct,

i didn't think of this, and was always thinking that redistributable is not working correctly,

Thanks,
Yash
[no name] 11-Jul-12 8:08am    
This question lacks sufficient information for an answer. It simply supplies a hypothesis:
"if i am not using this method _beginthreadex, then my application runs correclty"
What does this mean - if you don't use threading, if you use another type of threading - whatever?
Where do you start the thread, what's in the thread function etc?
What have you done to try to diagnose this bug - debugger?
Have you set a breakpoint in your thread function?
Sergey Alexandrovich Kryukov 11-Jul-12 14:18pm    
Exactly.
--SA

1 solution

CMyClass::threadFunction needs to be a static function of the class, since the _beginthreadex does not understand the hidden 'this' pointer.

I usually pass the pointer to the class as the argument to _beginthreadex so that inside the static function you can convert over to calling a member function via use of the pointer.

Search CodeProject for _beginthreadex and I am sure you will find some examples.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 10-Jul-12 16:56pm    
It does not answer the question, because OP already uses a static thread method, but this is know reasonable practice which is good to understand, my 4.
--SA
[no name] 11-Jul-12 8:09am    
Generous.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900