[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

C/C++ tråde



Her er et eksempel på tråde fra Linux Journal # 61:

/*
 * Filename:  thread.c
 */

/* You can compile this program with:
 * gcc -Wall -D_REENTRANT -o thread thread.c -lpthread
*/

/* We always need to include this header file for the threads */
#include <pthread.h>
#include <stdio.h>
#include <stdlib>

/* this is the prototype for our thread function */
void *mythread(void *data);
/* We must initialize our mutex */
pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
/* This will be a shared variable between all of the threads */
int x = 0;
int main(void)
{
 /* This will be an array holding the threads ids for each thread */
 
 /* We keep track of all the tids so that we can call
  * pthread_join() later to retrieve the return value
  * from the thread
 */
 pthread_t *tids;
 int i;
 /* We will now create the 10 threads. Each thread will
  * increment x until x is 4000.
  * On our last argument ot pthread_create we could
  * have passed an argument to the thread function
 */
 for(i=0;i<10;i++)
 { 
    pthread_dreate(&tids[i], NULL, mythread, NULL);
 }
 /* We will now wait for each thread to terminate */
 for(i=0;i<10;i++)
 {
 /* this will block until the specified thread finishes execution.
  * Our second argument to pthread_join can be a pointer that will have the return
  * value of thread stored in it
 */
    pthread_join(tids[i], NULL);
    printf("Thread id %ld returned\n", tids[i]);
 }
 return(1);
}

/* This is our actual thread function */
void *mythread(void *data)
{
 while(x<4000)
 {
    /* We will try to lock the mutex. If another thread already has it locked, we
     * will block until it is available again. After you first run this program,
     * you should comment out the lock/unlock lines in this function so that you
     * can see why you need mutexes.
    */
    pthread_mutex_lock(&count_mutex);
    x++;
    /* We will have it print out the thread ID and the value of x */
    printf("Thread id%ld: x is now %d\n",pthread_self(), x);
    /* We will now release the mutex so that another thread gets the chance to run */
    pthread_mutex_unlock(&count_mutex);
 }
 /* We can return a pointer. Whatever pointer we return can later be retrieved using
  * the pthread_join function
 */
 pthread_exit(NULL);
}

Venlig hilsen
Thomas Rasmussen


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 20:07 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *