# sample_start_connected_task

Initialize the task descriptor and spawn a task (thread).

### For an overview see page Samples Helper Functions in C

### Prototype

        unsigned int sample_start_connected_task( /*IN*/ sample_task_t * descr,
                                /*OUT*/ sample_task_proc_t task, )
                                /*IN*/ const char * db_name,
                                /*IN*/ void * param )

### Arguments

descr | The task descriptor to initialize:
      typedef struct tag_sample_task
      {
        void               * param;
        unsigned int         id;
        volatile int         stopped;
        const char         * db_name;
        mco_db_h             db_connection;
        sample_task_proc_t   proc;
        mco_thread_t       * t;

      } sample_task_t;
task | A pointer to the task (thread) procedure to start
db_name | The name of the database
param | An optional parameter structure passed into the task descriptor `descr`

### Description

This function initializes the task descriptor by setting the thread procedure `proc`, the `db_name` and `param` elements, then calls `mco_create_thread()` to spawn the thread.

### Return Codes

unsigned int | The value 1 if the thread was successfully started; otherwise zero

### Example

        #include <common.h>
        #include <transdb.h>
        const char * db_name = "transdb";

        #define N_THREADS 2
        static sample_task_t tasks[N_THREADS];
        ...

        void task1( sample_task_t * descriptor )
        {
            MCO_RET rc;
            uint4 i;
            for (i = 0; i < n_iterations; i++)
            {
                mco_trans_h t;
                A a;
                char tag[64];
                rc = mco_trans_start(descriptor->db_connection, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t);
                if ( MCO_S_OK == rc )
                {
                    sprintf(tag, "Stream 1, rec %u\n", i);
                    mem_write(tag);
                    rc = A_new(t, &a);
                    rc = A_ui4_put(&a, i);
                    rc = A_tag_put(&a, tag, strlen(tag));
                    sample_sleep(nap_duration1);
                    rc = mco_trans_commit(t);
                }
            }
        }

        int main(int argc, char* argv[])
        {
            MCO_RET rc;
            mco_db_h db;
            sample_memory_t dbmem;

            mco_runtime_start();

            rc = sample_open_database( db_name, transdb_get_dictionary(), DATABASE_SIZE, CACHE_SIZE,
                        MEMORY_PAGE_SIZE, PSTORAGE_PAGE_SIZE, 1 + N_THREADS, &dbmem );
            if ( MCO_S_OK == rc )
            {
                rc = mco_db_connect(db_name, &db);
                if ( MCO_S_OK == rc )
                {
                    sample_start_connected_task(&tasks[0], task1, db_name, 0);
                    ...
                }
            }
            ...
        }