# multi_task

> ## Directory

> > eXtremeDB_rt/samples/multi_task

> ## Description

> > Demonstrates multi-threaded usage of an in-memory database

> ## Used API

> > mco_db_connect(), mco_db_disconnect(), sample_open_database(), sample_close_database(), sample_start_task(), sample_join_task()

> ## Code snippet

> >         void a_task ( sample_task_t * descriptor ) {
> >           MCO_RET rc;
> >           char msg[32];

> >           /* Every task has to make its own connection */
> >           mco_db_h connection; /* Connection handle */
> >           /* Connect to it by name */
> >           rc = mco_db_connect( db_name, &connection );

> >         ...

> >           if ( MCO_S_OK == rc ) {
> >             /* Database successfully connected */

> >             /* Perform database tasks...*/

> >             /* Don't forget to disconnect when done */
> >             rc = mco_db_disconnect( connection );

> >         ...
> >             sample_rc_check(msg, rc );
> >           }
> >         }

> >         ....

> >           rc = sample_open_database( db_name, simpledb_get_dictionary(), DATABASE_SIZE, CACHE_SIZE,
> >                                        MEMORY_PAGE_SIZE, PSTORAGE_PAGE_SIZE, N_TASKS, &dbmem );
> >           sample_rc_check("\tOpen database", rc );

> >           if ( MCO_S_OK == rc ) {

> >             /* Task descriptors */
> >             sample_task_t task[N_TASKS];
> >             unsigned int  i;

> >             /* Start the tasks */
> >             for( i=0; i<sizeof(task)/sizeof(task[0]); i++ ) {
> >         #ifdef MCO_PLATFORM_X64
> >                 unsigned long long l = i;
> >         #else
> >                 unsigned int  l = i;
> >         #endif
> >                 sample_start_task( &task[i], a_task, (void*) l );
> >                 printf("\n\tTask %d started", i );
> >             }

> >             /* Wait for completion */
> >             for( i=0; i<sizeof(task)/sizeof(task[0]); i++ ) {
> >                 sample_join_task( &task[i] );
> >                 printf("\n\tTask %d joined", i );
> >             }

> >             /* Close the database */
> >             sample_close_database( db_name, &dbmem );

> ## What to expect as an output

> >     	Open database : Successful
> >     	Task 0 started
> >     		Connect Task 0 : Successful
> >     		Disconnect Task 0 : Successful
> >     	Task 1 started
> >     		Connect Task 1 : Successful
> >     		Disconnect Task 1 : Successful
> >     	Task 2 started
> >     	Task 3 started
> >     		Connect Task 2 : Successful
> >     		Disconnect Task 2 : Successful
> >     		Connect Task 3 : Successful
> >     		Disconnect Task 3 : Successful
> >     	Task 4 started
> >     		Connect Task 4 : Successful
> >     	Task 0 joined
> >     		Disconnect Task 4 : Successful
> >     	Task 1 joined
> >     	Task 2 joined
> >     	Task 3 joined
> >     	Task 4 joined

> >     	Close database : Success