Results 1 to 5 of 5

Thread: can i use stl type vector as the shared memory of two threads?

  1. #1
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default can i use stl type vector as the shared memory of two threads?

    I try it , at least now it works well.
    The program will be running almost all day, the size of vector could be large,
    Can I use vector as the shared memory of two threads? I use QThread.!

    ps: one thread is producer, the other is consumer.The producer emit signal when the shared vector has some data, the consumer consume the data and call clear() of the vector.

    Thank you in advance
    Last edited by calmspeaker; 29th December 2008 at 03:04.
    Jerry

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can i use stl type vector as the shared memory of two threads?

    You can use anything as long as you synchronize the access.
    A std::vector is not thread-safe (nor are QList, QMap, ...), so you have to use a QMutex or something like that to synchronize access. (Note: you might use a QList or whatever just as well.)

    HTH

  3. #3
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: can i use stl type vector as the shared memory of two threads?

    er, another question:
    is vector or qvector as efficient as circular buffer in the producer/consumer example in qt doc ?
    Jerry

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: can i use stl type vector as the shared memory of two threads?

    depends on how you use it: the vector itself is about as efficient as it can get (as long as you do not add/remove elements (pop_front()).
    The important thing for efficiency might be locking: do you lock the whole vector or just the critical parts of it? If you lock the whole vector then you restrict concurrency more than perhaps necessary.

    The most efficient thing is to use a static array and implement a circular buffer on it. On the other hand, the tiny loss in speed when using higher level structures might be negligible when writing an app that is not speed critical but where reliability and understandability of the code are more important.

    HTH

  5. The following user says thank you to caduel for this useful post:

    calmspeaker (30th December 2008)

  6. #5
    Join Date
    Oct 2008
    Posts
    37
    Thanked 2 Times in 2 Posts

    Default Re: can i use stl type vector as the shared memory of two threads?

    You might also want to take a look at boost's circular buffer. I haven't done anything practical with it - but it may be worth consideration.

Similar Threads

  1. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  2. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  3. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.