0

I want to construct a sqlite table in my program(linux C):

it has 3 columns/fields:

1 ip/port pair, 2 a FIFO queue pointer, 3 a process id or thread id

I'm not familiar with sqlite now, If I create a sqlite table, is it possible:

when a row is inserted into the sqlite table, a one-time timeout timer starts, if no expected event(when a row is created, the 3rd field is null, it waits for a process or thread to fill the thrid field.) happends within the timeout, the row is deleted. The timeout timer is one-time, so it is only used for once after the row is inserted into the table.

if so, are there any similar source codes? thanks!

misteryes
  • 2,167
  • 4
  • 32
  • 58

1 Answers1

0

A database is meant for persistent storage. Instead of adding then deleting from DB, use program memory as a temporary storage. You can break your process in four steps:

  1. place the objects to be completed in a queue, from the main thread.
  2. fire or notify the thread which should fill missing fields. The worker thread will read and modify objects from the queue and will also terminate after 1s.
  3. in the main thread, wait 1s (or wait for the worker thread to finish).
  4. check the objects inside the queue from the main thread: if the worker thread managed to fill the missing fields, commit the objects from the queue to the database. Otherwise, discard them from the queue.

You will need a thread safe queue, of course. Refer to this Stackoverflow answer.

Community
  • 1
  • 1
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80