Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Writing Vector object into a .txt file

Writing Vector object into a .txt file

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsgame-devannouncement
25 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jochen Arndt

    Just call Sleep() with a time out value of 1000 / frequency[Hz]. A better implememntation would use WaitForSingleObject() with the same time out value and a handle to a terminate thread event so that you can terminate the thread:

    UINT worker_thread_func(LPVOID pParam)
    {
    // pParam is usually a pointer to a C++ class to which this thread belongs
    // that is passed when creating the thread.
    // Cast the pointer to get access to member vars.
    MyClass *pThis = (MyClass *)pParam;
    HANDLE hKillEvent = pThis->m_hKillEvent;
    while (1)
    {
    if (WAIT_OBJECT_0 == WaitForSingleObject(hKillEvent, TIME_OUT_VAL))
    break;
    // Perform periodic task here
    }
    return 0;
    }

    M Offline
    M Offline
    Member 9350237
    wrote on last edited by
    #21

    Hi Jochen, I still didn't get idea of using WaitForSingleObject() as a timer. Basically WaitForSingleObject is a predefined function under windows.h? right?. Can you bit explain how exactly this can be embedded with a project?

    J 1 Reply Last reply
    0
    • M Member 9350237

      Hi Jochen, I still didn't get idea of using WaitForSingleObject() as a timer. Basically WaitForSingleObject is a predefined function under windows.h? right?. Can you bit explain how exactly this can be embedded with a project?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #22

      It is a Windows API function that is used with threads. It will suspend the thread until the event specified by the passed handle occurs or the time out time has elapsed. Using threads is an advanced topic. So you should read about it first. A starting point may be Using Worker Threads[^]. Your requirement was to execute specific tasks in fixed intervals. This can be done by Windows timers from within your main (GUI) thread. But this will block the main thread for the task execution time. With short intervals, this will lead to delayed user input (mouse and keyboard actions are not performed immediately). To avoid this, a worker thread can be used.

      M 1 Reply Last reply
      0
      • J Jochen Arndt

        It is a Windows API function that is used with threads. It will suspend the thread until the event specified by the passed handle occurs or the time out time has elapsed. Using threads is an advanced topic. So you should read about it first. A starting point may be Using Worker Threads[^]. Your requirement was to execute specific tasks in fixed intervals. This can be done by Windows timers from within your main (GUI) thread. But this will block the main thread for the task execution time. With short intervals, this will lead to delayed user input (mouse and keyboard actions are not performed immediately). To avoid this, a worker thread can be used.

        M Offline
        M Offline
        Member 9350237
        wrote on last edited by
        #23

        There is class precision clock included in the library. So I can use this for getting system time. But my doubt is if I've the 'time', with a conditional statement I should be able down sample data writing to a sampling rate of 100hZ??!. If I've time stamp at rate of 8KHz then how to formulate a conditional statement to write data at 100Hz??

        J 1 Reply Last reply
        0
        • M Member 9350237

          There is class precision clock included in the library. So I can use this for getting system time. But my doubt is if I've the 'time', with a conditional statement I should be able down sample data writing to a sampling rate of 100hZ??!. If I've time stamp at rate of 8KHz then how to formulate a conditional statement to write data at 100Hz??

          J Offline
          J Offline
          Jochen Arndt
          wrote on last edited by
          #24

          I can't answer this without knowing the library. That depends on the library, the implementation of that 'clock' and if it can generate some kind of signal.

          M 1 Reply Last reply
          0
          • J Jochen Arndt

            I can't answer this without knowing the library. That depends on the library, the implementation of that 'clock' and if it can generate some kind of signal.

            M Offline
            M Offline
            Member 9350237
            wrote on last edited by
            #25

            Just got some information on data logging for haptic thread. It's basically a data structure for the buffering of data on a haptic thread but I didn't understood completely how to use this for my application. It's basically a header. I'm attaching it here:

            /******
            *
            * Written by Dan Morris
            * dmorris@cs.stanford.edu
            * http://cs.stanford.edu/~dmorris
            *
            * You can do anything you want with this file as long as this header
            * stays on it and I am credited when it's appropriate.
            *
            ******/

            #ifndef _BLOCK_LINKED_LIST_H_
            #define _BLOCK_LINKED_LIST_H_

            // This is a custom data structure that I used for buffering data on
            // its way out to disk. It is not a random access data structure;
            // it just buffers up objects of type T and flushes them out to disk.
            //
            // If you're using flusher and writer threads, the general protocol
            // is to only allow the writer to call the push_back function, and
            // let the reader use the safe_flush function to do its flushing.
            // Then at some point when the writer is done forever, the reader can
            // call flush() to flush the remaining blocks that were currently in use
            // by the writer.
            //
            // This is a lock-free solution to the producer-consumer problem with the
            // constraint that immediate consumption is not important.
            //
            // This addresses a common access pattern for applications that have a time-
            // critical thread (e.g. a haptics thread) that generates data that
            // needs to be logged to disk. This approach avoids any disk access or
            // mutex access on the high-priority thread. I've used this data
            // structure for haptics (where a high-priority thread generates forces)
            // and neurophysiology/psychophysics (where a high-priority thread
            // controls an experiment).
            //
            // Note that in this mode, the "flusher" thread should _not_ also put
            // data into the array; i.e. the writer thread "owns" the push_back
            // function.

            // The node type for the linked list
            template class block_linked_list_node {
            public:

            // An array of T's
            T* data;
            block_linked_list_node* next;
            block_linked_list_node() {
            data = new T[chunk_size];
            next = 0;
            }
            ~block_linked_list_node() {
            delete [] data;
            }
            };

            // The data structure itself
            template class block_linked_list {
            public:

            // The head of the list
            block_linked_list_node* head;

            // The number of T's in the list
            size_t total_count;

            // A pointer to the curren

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups