CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    1

    Implementing queue as a class and threads

    Hi all,

    my requirement is there is a function which creates one thread and passes the data for further processing which looks like
    void MyFunc(char *data)
    {
    CreateThread(NULL,0,build_data,(void *)data,0,NULL);
    }
    Now i want to modify the above code.

    I want to introduce a queue class and want to create two threads so that 1st thread can invoke a function which writes the data to the queue and the 2nd thread reads the data from the queue. Please introduce mutex if required.
    Can you tell me how to implement the above in VC++.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: Implementing queue as a class and threads

    It's very simple, you just lock a critical section in every function that accesses the queue.

    Code:
    CCriticalSection cs;
    
    void read_queue() {
      cs.Lock();
      queue.read( ...
      cs.Unlock();
    }
    Nobody cares how it works as long as it works

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