CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2002
    Posts
    8

    Unhappy needed thread functionality in simple C

    I have looked in C books but I cannot find threads available in C. So I guess I will have to use someone’s library to get thread functionality in my C programs. Can some please tell me from where can I download such a library? My basic problem is that I have made a real time scheduler in C with custom functions working as threads, the problem is that in order to have complete functionality of real time one must be able to stop the function at any time and latter resume them. That functionality is only possible with threads but simple C does not has this functionality built in. I will be compiling on windows in visual C compiler as a consol app, but I cannot use visual C threads. Please Help.
    Still if such a solution is not possible then I will have to make a sort of a parser that will parse the functions and perform every single addition, multiplication, and division through a set of variables that I will consider registers. By storing those registers and resuming their value I might be able to achieve thread functionality. But there is another problem with that; firstly I store the functions as function pointers in an array, and latter after scheduling I pick them up from there. So is there a way that I can convert the contents of the function by that function pointer to a string so that latter I can parse the contents of the function or do I have to follow another approach. Please help with this problem as my deadline is nearing.

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417
    why can't you use _beginthread() and _endthread(). Look these function up on MSDN for an example *.c program.

  3. #3
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Threads are not part of C/C++.

    Look up CreateThread on MSDN for thread info.

    Microsoft also do something called fibers (Note the US spelling if you are used to spelling fibre the British way). With fibers, you control the scheduling. With threads, Windows controls the scheduling. Look for CreateFiber on MSDN and go from there.
    Succinct is verbose for terse

  4. #4
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Hello,

    I think that this developer is trying to simulate the essential portions of a pure cooperative multitasking operating system using threads, where each thread represents a task in the multitasking system. In this fashion, one simulates the saving/restoring of the CPU-context upon "task"-switch (simulated with thread sleep, wait, etc).

    Is this your developmental goal?

    I have experience with this type of simulation. It is straight forward using threads synchronized in a clearly defined fashion.

    If you are seriously considering doing this and although threads are not part of the C or C++ language, it is not really all that difficult.

    Respond if you are interested in discussing this topic further.

    Chris.

    You're gonna go blind staring into that box all day.

  5. #5
    Join Date
    Jul 2002
    Posts
    8

    Thumbs up

    You are absolutely right Chris. Basically the s/w I am trying to make is supposed to be a RTOS (REAL TIME OPERATING SYSTEM) supposedly for embedded devices. Although I do not have a particular chip, as I cannot afford one, therefore it is generic and I will simulate it on pc, preferably on windows; as much of the coding I have done is in the visual C 6 compiler. But the functionality should be as close to being independent, otherwise it won’t be much of an OS. The second reason is as most of the RTOS for embedded devices are in simple C; therefore I made mine in C as well. Therefore it is structured; hence mixing it with MFC will cause problems. The rest of the problem is as it is described in my first question above.
    If you need more explanation or my code I would be willing to share it. I appreciate your co-operation and would welcome further suggestions.

  6. #6
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    Will extract and provide appropriate sample code...
    Chris.

    You're gonna go blind staring into that box all day.

  7. #7
    Join Date
    Jul 2002
    Posts
    8
    /*
    The required data types and function prototypes for explanation are given below.
    What happens is that the programer writes the threads the relative deadlines for
    which are stored in a array. The function pointers of each of these threads or
    functions are stored in another array of type function pointers. The threads or
    function pointers are put in a doubly linked list according to their deadlines.From
    this linked list are the function pointers transfered to a execution stack by the
    schedule function, where the top thread function pointer is executed and then
    removed by a thread_end_instance function. The problem is that inorder for this to
    be a real time scheduler, if a thread with a higher priority and lesser deadline
    is entered while execution of any other thread, the executing thread should be
    stopped and stored. after the execution of the important thread it can be resumed.
    But this is not possible without real thread functionality. So here is the problem.
    what can be the possible solutions.
    */

    //Task relative deadlines in seconds
    clock_t th_reldline[]={3000,1000,2000,4000,5000,6000,7000,8000,9000,10000,11000,12000,14000,13000,15000,16000,6000};
    unsigned int th_ready_prio[]={14,16,15,13,12,11,10,9,8,7,6,5,3,4,2,1,11}; //Task preemption level

    //structure for the ready queue and the execution stack
    struct node2
    {
    struct node2 *prev;
    signed int index;//will store the thread or function number reflected in the function pointer array
    void (*fn_ptr)();//the function pointer for every element
    struct node2 *next;
    };

    //following are the function prototypes
    void thread0(void);
    void thread1(void);
    void thread2(void);
    void thread3(void);
    void thread4(void);
    void thread5(void);
    void thread6(void);
    void thread7(void);
    void thread8(void);
    void thread9(void);
    void thread10(void);
    void thread11(void);
    void thread12(void);
    void thread13(void);
    void thread14(void);
    void thread15(void);
    void thread16(void);
    //array containing the pointers of threads
    void (*th_bd_ptr[17])() = {thread0,thread1,thread2,thread3,thread4,thread5,
    thread6,thread7,thread8,thread9,thread10,thread11,
    thread12,thread13,thread14,thread15,thread16};
    void rq_insert(signed int t,struct node2 **f,struct node2 **r);
    //rq2stk_exchange() takes thread from ready queue and puts it on top of the stack.
    signed int rq2stk_exchange(struct node2 **f,struct node2 **r,struct node2 **s);
    void system_scheduler(void);//schedules the threads for one cycle
    void end_thread_instance(struct node2 **s,struct node2 **ss,struct node2 **ss1,struct node2 **ss2);//Ends every thread and does the related work
    void stk2blk_exchange(struct node2 **s,struct node2 **b);//puts thread from stack to blocked queue
    void make_thread_ready(signed int t, unsigned char nact);//puts the thread in the ready queue

  8. #8
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    onlyhuman23:

    You want to simulate full-preemptive multitasking. I only have simulations of full cooperative multitasking readily available. I wrote the sample included here a few years ago. It is in plain C using the Win32 API.

    Your development goals differ from the sample solution in two main areas:
    1) Your task control blocks are stored in a linked list, the sample code stores them in a static array.
    2) You want full preemptive simulation: Tasks can interrupt each other. The enclosed simulation in pure cooperative.

    The first difference is merely cosmetic, the second quite a bit more. Nevertheless, take a look at the sample. Just build the project and run it. It's simplicity might "win you over".

    Unfortunately I do not have time to really delve into the problem at hand right now, and I feel that a full-blown discussion about RTOS simulations (although toatally interesting to me) might be beyond the scope of this forum.

    Happy development.
    Chris.

    Attached Files Attached Files
    You're gonna go blind staring into that box all day.

  9. #9
    Join Date
    Jul 2002
    Posts
    8
    I would just like to ask you one more thing. I was under the impression that thread mechanism is compiler dependant rather than OS dependant; is this true or not. Are both things possible, for example to have a OS based thread mechanism and a compiler based one too.

  10. #10
    Join Date
    May 2001
    Posts
    472
    Threads are managed by OS entirely in Windows. There isn't much a compiler has to do. In Unix, threads had to be written manually, without much OS help. To ease the porting of UNIX applications, Windows implements fibers, which are much like threads, except that the scheduling is implemented by the application.
    Ce n'est que pour vous dire ce que je vous dis.

  11. #11
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    onlyhuman23:

    The comments of Alexy B are correct.

    In Unix/Linux using GCC/CC, one can develop multithreaded applications using Posix threads (http://www.amazon.com/exec/obidos/AS...646980-0930441). There is library support for Posix threads using the pthreads (http://www.amazon.com/exec/obidos/AS...646980-0930441). In Windows using VC6 or VC.NET, one can develop multithreaded applications using Windows threads (http://www.amazon.com/exec/obidos/AS...646980-0930441), Windows fibers are also described within this book. Both books will be on the shelf of a good technical bookstore.

    Threads are managed by the operating system. Good compilers tend do support libraries of function calls which allow the management (creation, suspension, continuation, synchronization, etc) of threads.

    Please note that the simulation code provided above is absolutely not a typical use of multithreading. It is, rather, a tutorial on simulating basic elements which are common among many cooperative multitasking RTOS's.

    Chris.

    You're gonna go blind staring into that box all day.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured