problem about critical-section-Collection of common programming errors


  • eat_a_lemon
    c multithreading pthreads race-condition critical-section
    If I define a thread function that reuses another function that the main thread also uses….is it possible that there can be a race condition? Are the local variables in the same function shared across threads? In this case the function do_work is used in both the thread_one thread and the main thread. Can the local variable x in the function do_work be modified by both threads so it creates an unexpected result? void *thread_one() {int x = 0;int result;while(1) {for(x=0; x<10; x++) {res

  • Martin Ba
    c++ windows winapi critical-section
    I’m trying to add some debug checking for a CRITICAL_SECTION unlocking code, and I tried the following:…if (m_pCritSect) {ASSERT(m_pCritSect->OwningThread == GetCurrentThreadId());LeaveCriticalSection(m_pCritSect);} }From debugging CRITICAL_SECTIONS (With VS 2005, mostly on WindowsXP) I “know” that the value of OwningThread (member of the RTL_CRITICAL_SECTION structure defined in winnt.h) is the value of th ID of the thread holding the lock.However thread IDs are represented by DWORD (typed

  • Andy Dent
    windows critical-section
    I am refining a large body of native code which uses a few static critical sections and never calls DeleteCriticalSection, leaving them to process exit to clean up.There are no leaks and no concerns about the total number of CS getting too high, I’m just wondering if there are any long-term Windows consequences to not cleaning them up. We have regression test suites that will launch a program thousands of times a day, although end users are not likely to do anything like that. Because of the ran

  • Benjamin
    windows winapi system mutex critical-section
    CRITICAL_SECTION locking (enter) and unlocking (leave) are efficient becauseCS testing is performed in user space without making the kernel system call thata mutex makes. Unlocking is performed entirely in user space, whereas ReleaseMutex requires a system call.I just read these sentences in this book. What the kernel system call mean? Could you give me the function’s name?I’m a English newbie. I interpreted them like this.CS testing doesn’t use a system call. Mutex testing uses a system call.(B

  • dario_ramos
    c++ windows multithreading visual-c++ critical-section
    My program is randomly crashing in a small scenario I can reproduce, but it happens in mlock.c (which is a VC++ runtime file) from ntdll.dll, and I can’t see the stack trace. I do know that it happens in one of my thread functions, though.This is the mlock.c code where the program crashes:void __cdecl _unlock (int locknum) {/** leave the critical section.*/LeaveCriticalSection( _locktable[locknum].lock ); }The error is “invalid handle specified”. If I look at locknum, it’s a number larger than _

  • Andreas Rejbrand
    multithreading delphi thread-safety critical-section interlocked-increment
    Please pardon my slightly humorous title. I use two different definitions of the word ‘safe’ in it (obviously).I am rather new to threading (well, I have used threading for many years, but only very simple forms of it). Now I am faced with the challange of writing parallal implementations of some algorithms, and the threads need to work on the same data. Consider the following newbie mistake:constN = 2;varvalue: integer = 0; function ThreadFunc(Parameter: Pointer): integer; vari: Integer; beg

  • xezon
    c++ multithreading critical-section
    I build a little application which has a render thread and some worker threads for tasks which can be made nearby the rendering, e.g. uploading files onto some server. Now in those worker threads I use different objects to store feedback information and share these with the render thread to read them for output purpose. So render = output, worker = input. Those shared objects are int, float, bool, STL string and STL list.I had this running a few months and all was fine except 2 random crashes du

  • Science_Fiction
    c++ synchronization windbg critical-section
    I have an issue (crash dump) where my Critical Section is being destroyed but after inspecting the LockCount I note that there is 1 thread waiting on it (it seems that the thread has been woken, but not yet entered, since lock status is Not Locked).I want to see what thread has been woken. I know the Critical Section has a queue of waiting threads, if I could dump this queue/list structure I should be able to answer my question, any idea’s on what I could do?

  • Wilson F
    c windows critical-section
    Is there any circumstance in which the LockCount field of a RTL_CRITICAL_SECTION structure in Windows can legitimately be negative?We’re tracking a VERY elusive crash and one symptom we’re seeing is a CS with a negative LockCount. At the time of the crash, the count is -6, but it seems to routinely be -1, -2, etc.Before go chasing off after that on the assumption that it is a Very Bad Thing for this to occur, I just want to verify that that assumption is correct. I can find little to no informat

  • PaulB
    .net multithreading garbage-collection deadlock critical-section
    We have a problem with our application that is using a mixture of managed (C#) and unmanaged (C++) code. Basically we have a exe that invokes a bunch of assemblies and one of these assemblies is a MC++ wrapper of our C++ library. The application is a console app. Most of the time it work fine but occasionally it hangs without any errors or exceptions.Using memory dumps and symbols we’ve been able to do some diagnosis in WinDbg but I’m not really sure what we are seeing is a deadlock or not. I’ve

  • Chris A.
    c++ locking openmp critical-section
    I have two piece of C++ code running on 2 different cores. Both of them wirte to the same file. How to use openMP and make sure there is no crash?

  • Rajesh Subramanian
    multithreading mfc critical-section
    class MyClass {……CCriticalSection m_Cs;void MyFunction(){m_Cs.Lock();…..m_Cs.Unlock(); }MyClass::~MyClass(){….} };I am using Critical Section in my class as above, myFunction is called by Thread 1, and when myFunction is in progress Thread 2 is deleting the object. So Unlock is crashing.So I decided to modify MyClass destructor as belowMyClass::~MyClass(){m_Cs.Lock();m_Cs.Unlock(); ….}This resolves my crash, because when the Thread 1 is accessing MyFunction CriticalSection is locked,

  • Ayesha Hassan
    c windows multithreading synchronization critical-section
    I have two threads on my Server, one Worker thread and one Receiving Thread. There is a shared Queue containing list of all active Clients for this Server i.e. Queue has IP and Port Number of each Active ClientWorker Thread is in Critical Section and keeps popping data out of the queue and sends a message to each IP and Port Number popped out.Receiving Thread only waits for new connections and as soon as a new client in addition to active clients arrives, Receiving Thread immediately wants Worke

  • Brian Kelly
    c++ visual-studio-2010 msvcrt critical-section static-initializer
    I’ve got this warning recently (VC++ 2010)warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminatorsI’m assuming this is the Critical Section. It’s been a while since my Operating Systems course, so I can’t really figure out what this means. If I remember right, the Critical Section works with shared resources. So how is this warning related and what does it mean exactly?

  • Doug T.
    c++ multithreading winapi critical-section waitformultipleobjects
    I’m working through an example of protecting a global double using mutexes, however I get the error -Unhandled exception at 0x77b6308e inLab7.exe: 0xC0000005: Access violationwriting location 0x00000068.I assume this is related to accessing score? (The global double) #include <windows.h> #include <iostream> #include <process.h>double score = 0.0; HANDLE threads[10]; CRITICAL_SECTION score_mutex; unsigned int __stdcall MyThread(void *data) {EnterCriticalSection(&score

  • Mike O’Connor
    c++ file-io critical-section
    I need to write a class which reads from and writes to a file. When I do a write operation, read should not take place and also vice versa. Can I use a single critical section object for that? Like this:FileWorker.hclass FileWorker { public:FileWorker();void WriteIntoFile(const char* fileName, const char* stringToWrite);void ReadFromFile(const char* fileName, char* stringToRead, int* stringLength);~FileWorker(); };FileWorker.cpp#include <windows.h> #include “FileWorker.h”static CRITICAL_SE

  • lowliet
    c++ multithreading winapi synchronization critical-section
    I’ve got thread executing commands from listdo {commandExec->criticalSection.EnterCS();if (!commandExec->commands.empty()){commandExec->ExecuteCommand(commandExec->commands.front());commandExec->commands.pop_front();}elsecommandExec->criticalSection.SuspendThread();commandExec->criticalSection.LeaveCS(); } while (commandExec->maintainCommandExecution);and second thread which adds commands to list:criticalSection.EnterCS(); commands.push_back(Command(code, parameters)); cr

  • Sergei Ousynin
    winapi critical-section c++builder-6
    Recently i tried to run an old app (written in Borland C++ Builder 6) on Windows 7 x64 and found that IO-thread was not working. Debugging showed the problem was in error-handling class not leaving critical section (indirectly, via a simple wrapper class), so every message added to log caused two calls of EnterCriticalSection but only one call of LeaveCriticalSection.It seems to me that this error should make the class unusable, but it’s one of the common classes actively used by original develo

  • curryage
    pthreads mutex signals critical-section
    I have the following piece of code in thread A, which blocks using pthread_cond_wait() pthread_mutex_lock(&my_lock); if ( false == testCondition ) pthread_cond_wait(&my_wait,&my_lock); pthread_mutex_unlock(&my_lock);I have the following piece of code in thread B, which signals thread Apthread_mutex_lock(&my_lock); testCondition = true; pthread_cond_signal(&my_wait); pthread_mutex_unlock(&my_lock);Provided there are no other threads, would it make any d

  • Shannon
    multithreading delphi critical-section
    I’m using several critical sections in my application. The critical sections prevent large data blobs from being modified and accessed simultaneously by different threads. AFAIK it’s all working correctly except sometimes the application hangs when exiting. I’m wondering if this is related to my use of critical sections. Is there a correct way to free TCriticalSection objects in a destructor? Thanks for all the answers. I’m looking over my code again with this new information in mind. Cheers!

Web site is in building