question on critical section
-
Hi. I have a thread that adds values to a vector variable in a class. also main program reads same thread an to display data in it. I havn't used critical section or other sync methods but program works ok, is it necessary to use critical section ? thanks.
-
Hi. I have a thread that adds values to a vector variable in a class. also main program reads same thread an to display data in it. I havn't used critical section or other sync methods but program works ok, is it necessary to use critical section ? thanks.
-
Hi, Because u r using multi-threading application you SHOULD sync ur work using critical sections, events, semaphores,... your program runs well now, but when the thread writes to the variable at the same time as the main thread reads it, the value read by the main thread will be out-of-date, it is not the current value of the variable. Furthermore, if you don't use the keyword volatitle in declaring shared variables, the compiler may perform wrong optimizations to speed the code up, and you may get wrong results. -Always take the safest way Regards, Mohammad Gdeisat
-
Hi, Because u r using multi-threading application you SHOULD sync ur work using critical sections, events, semaphores,... your program runs well now, but when the thread writes to the variable at the same time as the main thread reads it, the value read by the main thread will be out-of-date, it is not the current value of the variable. Furthermore, if you don't use the keyword volatitle in declaring shared variables, the compiler may perform wrong optimizations to speed the code up, and you may get wrong results. -Always take the safest way Regards, Mohammad Gdeisat
-
Another concern is a possible program crash. When vectors are changed, all iterators are invalidated. So if the main function has an interator and is in the process of reading the data, the thread could interrupt and add something to the vector. Now the main function has an invalid iterator. The problem with not using synch methods is that the errors may not be noticed for months. You could think all is well and then one day, your program crashes while doing the same thing it's been doing all along. Shawn