_beginthread()

This is a discussion on _beginthread() within the C++ Programming forums, part of the General Programming Boards category; When I use _beginthread(), it doesn't run the thread everytime. Is there a way to be sure the call to ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    20

    _beginthread()

    When I use _beginthread(), it doesn't run the thread everytime. Is there a way to be sure the call to another thread will run?

    Thanks,
    Matt

    Code:
    ...
    else if((!bRewarded) && (bICTA) && (bInRef)) {
    ldHotClk = ldStateCtr - ldHotRef;
    if(ldHotClk > iTime)	{						
    bRewarded = true;
    ++iRewardNum;
    bICTA = false;
    ldColdClk = 0;
    ldHoldColdClk = 0;
    ldHoldEndColdClk = 0;
    bOutRef = false;				
    _beginthread(SignalPlacePref, 0, NULL);
    }
    ...
    
    /*****************************************************/
    void SignalPlacePref(void)
    {
    	bThreadFunctCalled = true;
    	if(!bRewardiscontinue)	{
    		cbDBitOut(iBoardNum, iPortType, iBitNum1, 1);	//	Raise digital bit 1 to set Enable Hi
    		Sleep(5);										//	Wait to besure Enable is Hi before next step
    		cbDBitOut(iBoardNum, iPortType, iBitNum0, 1);	//	Raise digital bit 0 to trigger plt fdr
    		Sleep(10);										//	Ensure long pulse for triggering plt fdr
    		cbDBitOut(iBoardNum, iPortType, iBitNum0, 0);	//	Bring bits down
    		cbDBitOut(iBoardNum, iPortType, iBitNum1, 0);
    	}
    	_endthread();
    }
    /*******************************************************/

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    How have you determined that it does not run the thread everytime? A test case would be appreciated:

    http://jcatki.no-ip.org/fncpp/TestCase

    Also, what are you doing that requires threads. The major problem with threads is peopel seem to use them in places that they deffinatly shouldn't. Are you sure you ened threads here? They mostly complicate programs instead of make them simpler.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    20
    Thanks for the reply. In the code for the thread, I set bThreadFunctCalled = true. If the thread was not called, then bThreadFunctCalled = false. I look in an output file that writes out variables so I can see when bThreadFunctCalled = true. This coordinates with some of the other variables, but it doesn't work right (i.e. I no longer see bThreadFunctCalled = true)
    The code in the thread sets a bit HI on a PCI digital IO board which will trigger a pellet feeder. The feeder beeps when it is triggered. This isn't working because there is no pellet or beep.
    I need to do this because this is a real-time video tracking program and I need to process each frame within a small window of time or I'll drop a frame.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    Code:
    uintptr_t _beginthread( 
       void( __cdecl *start_address )( void * ),
       unsigned stack_size,
       void *arglist 
    );
    Try paying more attention to return values.

    > and I need to process each frame
    Are you sure?
    Is you machine even capable of processing each frame at say 20% of CPU time?
    And if you're deciding whether or not to create a thread on every frame, just how many threads have you created?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    If you need to process each frame within a window then threads are probably going to give you worse performance. The context switch between threads takes time. Especially if you are spawning a lot you are going to be spending more time goign between threads than actually processing. You can probably do this much simpler by breaking your algorithm into logical, short, chunks and swiching between them in a single thread. You avoid the threading here, thus context switches. You also don't have to worry about any issues that come with threads such as synchronization and deadlocks. Debugging will also most likely be much simpler. The hardest part is probably going to be deciding how much time to give each chunk at a time.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21