From: "Mr. X" Newsgroups: comp.os.msdos.djgpp Subject: Re: Thread safe programs. Date: Sun, 15 Feb 1998 04:11:13 -0800 Organization: Skylink Networks, Inc. (http://www.skylink.net./) Lines: 56 Message-ID: <6c6lpm$kbn$1@news.skylink.net> References: <3 DOT 0 DOT 16 DOT 19980214230158 DOT 5057866a AT hem1 DOT passagen DOT se> NNTP-Posting-Host: ppp163.max3.las-vegas.nv.skylink.net To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Peter Palotas wrote: >I was just wondering if someone could tell me what a thread-safe program >includes, i.e. how one would make a program/library thread safe. I know this >isn't really a DJGPP specific question, but isn't it an important task when >porting (and well, there is multithreading libs for DJGPP too). Typically by making up functions like lock_data(key) and unlock_data(key) where lock_data() is on a first come, first serve basis and unlock_data() only works when called by the current locker. The key is just an id for each possible caller, so that bob() can't unlock cindy()'s lock. Don't forget that each copy of code needs it's own key. Say you have 4 AI Tron light cycles all wanting to write to the screen, they may be 4 threads running the same code, but they each need their own id, so that they only write to the screen one at a time. Otherwise, they'd corrupt it, etc.... BTW - you don't actually need keys, but they can help catch odd bugs.... Of course, the object type way would be to have a class that owns it and provides functions to manipulate it, etc. This is the clean, portable and secure way to do it. Oh, did I forget to mention slow? ;) >Ofcourse not using any global or static data would make a program threadsafe >(or?), but what if you are writing a library that NEEDS static or global >data!? Can threads be disabled during the execution of a specific >function in all multi-threading systems? >I tried this, but I wasn't able to find any information about wether or not >it is a portable solution to assume that there is some way to disable >threads on all platforms from within the code, hence defining something >like "#define DISABLE_THREADS some_function();" and "#define ENABLE_THREADS >some_other_function();". Nah, you can use static, just so long as they are real static and you don't go changing them like many lax compilers let people do. Global needs lock() and unlock() functions. There is a very easy way to disable threads but I don't know if I should even mention it, it's called forbid() and it's a sin to use it, it totally hogs the CPU from everything else, includeing the OS, mouse driver, etc. Of course, M$ code acts like it's a collection of forbids..... >So if anyone could give me a good answer to this, I would be really >greatful, because my program really *needs* a global linked list, and I >want to know if I can somehow make sure it is thread safe on any >multi-threading platform. Do you *really* need a global linked list? :) I ask that because many (most?) people never learn to approach a problem in a truely non-linear manner. X