   Can anyone help?
  helloi've been writting a simple, not optimised yet, method of beening able to control what runs inside my thread. it's only an idea, but i want to try and see if i can add a form of frequency to how i schedule work in a thread. i use a multimedia timer to give me the ability to interrupt the threads flow and then change it. it's only an idea i am playing around with and its probably been done to death and better solutions found, but its part of the learning process.my current solution seems to work but i have one major issue, when-ever i call 'cout' i get an exception thrown:Unhandled exception at 0x10240a97 (msvcr90d.dll) in TestThreadTimer02.exe: 0xC0000005: Access violation reading location 0x00a8f000.can any one give me any idea of what i have done wrong. any help or advice would be much appreciated. i am using Visual Studio 2008. my code is:#include <Windows.h>#include <MMSystem.h>#include <iostream>#include <string>#include <exception>using namespace std;#pragma comment(lib, "winmm.lib")CRITICAL_SECTION cs;bool bProc00, bProc01;// ==========typedef struct __stack {void *ss_sp;size_t ss_size;int ss_flags;} stack_t;typedef CONTEXT mcontext_t;typedef unsigned long __sigset_t;typedef struct __ucontext {unsigned long intuc_flags;struct __ucontext*uc_link;stack_tuc_stack;mcontext_tuc_mcontext; __sigset_tuc_sigmask;} ucontext_t;int GetContext(HANDLE hThr, ucontext_t *pUCon){int nRet;pUCon->uc_mcontext.ContextFlags = CONTEXT_FULL;nRet = GetThreadContext(hThr, &pUCon->uc_mcontext);return (nRet == 0) ? -1: 0;}int SetContext(HANDLE hThr, ucontext_t *pUCon){int nRet;nRet = SetThreadContext(hThr, &pUCon->uc_mcontext);return (nRet == 0) ? -1: 0;}int MakeContext(ucontext_t *pUCon, void (*func)(), int argc, ...){int nI;va_list vaList;char *pSP;pSP = (char *)(size_t)pUCon->uc_stack.ss_sp + pUCon->uc_stack.ss_size;pSP -= argc * 8;if(pSP < (char *)pUCon->uc_stack.ss_sp){std::cout << "Error: MakeContext" << std::endl;return -1;}//pUCon->uc_mcontext.Eip = (unsigned long) func;pUCon->uc_mcontext.Esp = (unsigned long) pSP - 4;//pUCon->uc_mcontext.ContextFlags = CONTEXT_FULL;//return 0;}int SwapContext(HANDLE hThr, ucontext_t *pUConCurrent, ucontext_t *pUConNext){int nRet;if ((pUConCurrent == NULL) || (pUConNext == NULL)){std::cout << "Error: SwapContext1" << std::endl;exit(1);return -1;}//nRet = GetContext(hThr, pUConCurrent);//if (nRet == 0){nRet = SetContext(hThr, pUConNext);if (nRet != 0){std::cout << "Error: SwapContext3" << std::endl;}}else{std::cout << "Error: SwapContext2" << std::endl;exit(1);}//return nRet;}// ==========typedef struct My_Data{int nVal;int nRate;} MYDATA, *PMYDATA;#define NUM_THREADS 1#define NUM_PROCS 2PMYDATA pData[NUM_THREADS];HANDLE hThread[NUM_THREADS];DWORD dwGenericThread;int nC, nCount;MMRESULT FTimeID;//ucontext_t ProcContext[NUM_PROCS], MainContext;// ==========void Proc00(){int nI, nD = 0, nC = 0;//std::cout << "[1]";for (nI = 0; nI < 10; nI++){for (nD = 0; nD < 10000000; nD++){nC = (nD * nI) + nD;}//Sleep(pData->nRate);//std::cout << "1";}//std::cout << "{1}";bProc00 = false;while(true){//}//SuspendThread(hThread[0]);}void Proc01(){int nI, nD = 0, nC = 0;//std::cout << "[2]";for (nI = 0; nI < 10; nI++){for (nD = 0; nD < 10000000; nD++){nC = (nD * nI) + nD;}//std::cout << "2";//Sleep(pData->nRate);}//EnterCriticalSection(&cs);try{std::cout << "{2}";// << std::flush;}catch (char *e){}//LeaveCriticalSection(&cs);bProc01 = false;while(true){//}//SuspendThread(hThread[0]);}void CALLBACK TimerProc(UINT uTimerID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2){SuspendThread(hThread[0]);//std::cout << "+";if (nCount == 0){//if (bProc00){//std::cout << "=";SwapContext(hThread[0], &ProcContext[0], &ProcContext[1]);}nCount = 1;}else{//if (bProc01){//std::cout << "-";SwapContext(hThread[0], &ProcContext[1], &ProcContext[0]);}nCount = 0;}if (bProc00 != true && bProc01 != true){EnterCriticalSection(&cs);std::cout << " DONE " << std::endl << std::flush;;LeaveCriticalSection(&cs);SwapContext(hThread[0], &ProcContext[1], &MainContext);}//std::cout << ".";ResumeThread(hThread[0]);}DWORD WINAPI StartThread(LPVOID pVal){int nI, nD = 0, nC = 0;PMYDATA pData;pData = (PMYDATA)pVal;std::cout << "s";//Sleep(1000 * 10);std::cout << "f";return 0;}void main(void){UINT uDelay = 500;UINT uResolution = 1;DWORD dwUser = NULL;UINT uEvent = TIME_PERIODIC;MMRESULT FTimeID;int nSize1 = 0, nSize2 = 0;bProc00 = true;bProc01 = true;InitializeCriticalSection(&cs);//std::cout << std::endl << "Starting..." << std::endl << std::endl;//for (nC = 0; nC < NUM_THREADS; nC++){pData[nC] = new MYDATA;pData[nC]->nVal = nC;pData[nC]->nRate = (nC + 1) * 500;hThread[nC] = CreateThread(NULL, 0, StartThread, pData[nC], CREATE_SUSPENDED, &dwGenericThread);}for (nC = 0; nC < NUM_THREADS; nC++){SetThreadAffinityMask(hThread[nC], 1 << nC);}//GetContext(hThread[0], &MainContext);//GetContext(hThread[0], &ProcContext[0]);ProcContext[0].uc_stack.ss_size = 16 * 1024;ProcContext[0].uc_stack.ss_sp = malloc(ProcContext[0].uc_stack.ss_size);ProcContext[0].uc_stack.ss_flags = 0;MakeContext(&ProcContext[0], Proc00, 0);//GetContext(hThread[0], &ProcContext[1]);ProcContext[1].uc_stack.ss_size = 16 * 1024;ProcContext[1].uc_stack.ss_sp = malloc(ProcContext[1].uc_stack.ss_size);ProcContext[1].uc_stack.ss_flags = 0;MakeContext(&ProcContext[1], Proc01, 0);//nSize1 = sizeof(ProcContext[0]);nSize2 = sizeof(ProcContext[0].uc_mcontext);//SwapContext(hThread[0], &ProcContext[1], &ProcContext[0]);SetContext(hThread[0], &ProcContext[1]);nCount = 1;//timeBeginPeriod(1);for (nC = 0; nC < NUM_THREADS; nC++){ResumeThread(hThread[nC]);}//FTimeID = timeSetEvent(uDelay, uResolution, TimerProc, dwUser, uEvent);WaitForMultipleObjects(NUM_THREADS, hThread, true, INFINITE);DeleteCriticalSection(&cs);std::cout << std::endl << std::endl << "Terminating..." << std::endl;}[Edited by - jpetrie on May 21, 2009 7:05:30 PM]
  In the future, please use source tags when posting large blocks of code. You can edit your post to see how I added them for you.This error is indicating to you that you are accessing memory that you are not actually allowed to access. Typically this occurs when dereferencing a null, or invalid, pointer. It sounds like what you are trying to do is likely ill-advised, and may be responsible for corrupting memory in some fashion. Does this occur when you utilize std::cout in any fashion, or only from within one of your threads? Does it occur when you print anything, or only some custom object or pointer? For example does the very first line utilizing cout in main work?You will want to step through the code in your debugger to look for the access violation. If you are unfamiliar with the debugger, you can check out this article. Your IDE should also provide a way to show you the line that was executing when you caused the crash. This is typically one of the buttons in the dialog that shows up when the crash happens. Starting with a breakpoint there is usually the best option.   ;  sorry about not putting the code within the source tags.it only occurs from within the thread itself, from 'main' it prints fine. in fact this error only occurs when run within Visual Studio, when run from a console, everything runs fine; no errors or exceptions, all cout's work.i will try the debugger option you suggested.   ;  I don't think cout is threadsafe if that helps you any...   ;  so i have read from the MSDN library, cout is thread safe. it mentions an example of 2 threads using cout and warns of the possiblity of inter-mixing output texts.   